2 * This software was developed by the Software and Component Technologies
3 * group of Trimble Navigation, Ltd.
5 * Copyright (c) 1997, 1998, 1999, 2000 Trimble Navigation Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Trimble Navigation, Ltd.
19 * 4. The name of Trimble Navigation Ltd. may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY TRIMBLE NAVIGATION LTD. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL TRIMBLE NAVIGATION LTD. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * refclock_palisade - clock driver for the Trimble Palisade GPS
40 * For detailed information on this program, please refer to the html
41 * Refclock 29 page accompanying the NTP distribution.
43 * for questions / bugs / comments, contact:
44 * sven_dietrich@trimble.com
46 * Sven-Thorsten Dietrich
47 * 645 North Mary Avenue
48 * Post Office Box 3642
49 * Sunnyvale, CA 94088-3642
51 * Version 2.45; July 14, 1999
59 #if defined(REFCLOCK) && (defined(PALISADE) || defined(CLOCK_PALISADE))
61 #include "refclock_palisade.h"
62 /* Table to get from month to day of the year */
63 const int days_of_year [12] = {
64 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
68 const char * Tracking_Status[15][15] = {
69 { "Doing Fixes\0" }, { "Good 1SV\0" }, { "Approx. 1SV\0" },
70 {"Need Time\0" }, { "Need INIT\0" }, { "PDOP too High\0" },
71 { "Bad 1SV\0" }, { "0SV Usable\0" }, { "1SV Usable\0" },
72 { "2SV Usable\0" }, { "3SV Usable\0" }, { "No Integrity\0" },
73 { "Diff Corr\0" }, { "Overdet Clock\0" }, { "Invalid\0" } };
79 struct refclock refclock_palisade = {
80 palisade_start, /* start up driver */
81 palisade_shutdown, /* shut down driver */
82 palisade_poll, /* transmit poll message */
83 noentry, /* not used */
84 noentry, /* initialize driver (not used) */
85 noentry, /* not used */
86 NOFLAGS /* not used */
89 int day_of_year P((char *dt));
92 * palisade_start - open the devices and initialize data for processing
107 struct palisade_unit *up;
108 struct refclockproc *pp;
114 (void) sprintf(gpsdev, "COM%d:", unit);
116 (void) sprintf(gpsdev, DEVICE, unit);
122 fd = open(gpsdev, O_RDWR
128 fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
132 printf("Palisade(%d) start: open %s failed\n", unit, gpsdev);
137 msyslog(LOG_NOTICE, "Palisade(%d) fd: %d dev: %s", unit, fd,
141 tio.c_cflag = (CS8|CLOCAL|CREAD|PARENB|PARODD);
142 tio.c_iflag = (IGNBRK);
146 if (cfsetispeed(&tio, SPEED232) == -1) {
147 msyslog(LOG_ERR,"Palisade(%d) cfsetispeed(fd, &tio): %m",unit);
149 printf("Palisade(%d) cfsetispeed(fd, &tio)\n",unit);
153 if (cfsetospeed(&tio, SPEED232) == -1) {
155 printf("Palisade(%d) cfsetospeed(fd, &tio)\n",unit);
157 msyslog(LOG_ERR,"Palisade(%d) cfsetospeed(fd, &tio): %m",unit);
161 if (tcgetattr(fd, &tio) < 0) {
163 "Palisade(%d) tcgetattr(fd, &tio): %m",unit);
165 printf("Palisade(%d) tcgetattr(fd, &tio)\n",unit);
170 tio.c_cflag |= (PARENB|PARODD);
171 tio.c_iflag &= ~ICRNL;
174 if (tcsetattr(fd, TCSANOW, &tio) == -1) {
175 msyslog(LOG_ERR, "Palisade(%d) tcsetattr(fd, &tio): %m",unit);
177 printf("Palisade(%d) tcsetattr(fd, &tio)\n",unit);
183 * Allocate and initialize unit structure
185 up = (struct palisade_unit *) emalloc(sizeof(struct palisade_unit));
188 msyslog(LOG_ERR, "Palisade(%d) emalloc: %m",unit);
190 printf("Palisade(%d) emalloc\n",unit);
196 memset((char *)up, 0, sizeof(struct palisade_unit));
199 pp->io.clock_recv = palisade_io;
200 pp->io.srcclock = (caddr_t)peer;
203 if (!io_addclock(&pp->io)) {
205 printf("Palisade(%d) io_addclock\n",unit);
213 * Initialize miscellaneous variables
215 pp->unitptr = (caddr_t)up;
216 pp->clockdesc = DESCRIPTION;
218 peer->precision = PRECISION;
219 peer->sstclktype = CTL_SST_TS_UHF;
220 peer->minpoll = TRMB_MINPOLL;
221 peer->maxpoll = TRMB_MAXPOLL;
222 memcpy((char *)&pp->refid, REFID, 4);
225 up->unit = (short) unit;
226 up->rpt_status = TSIP_PARSED_EMPTY;
234 * palisade_shutdown - shut down the clock
249 struct palisade_unit *up;
250 struct refclockproc *pp;
252 up = (struct palisade_unit *)pp->unitptr;
253 io_closeclock(&pp->io);
260 * unpack_date - get day and year from date
276 /* Check month is inside array bounds */
277 if ((mon < 1) || (mon > 12))
280 day = dt[0] + days_of_year[mon - 1];
281 year = getint((u_char *) (dt + 2));
283 if ( !(year % 4) && ((year % 100) ||
284 (!(year % 100) && !(year%400)))
286 day ++; /* leap year and March or later */
293 * TSIP_decode - decode the TSIP data packets
310 unsigned short event = 0;
312 struct palisade_unit *up;
313 struct refclockproc *pp;
316 up = (struct palisade_unit *)pp->unitptr;
319 * Check the time packet, decode its contents.
320 * If the timecode has invalid length or is not in
321 * proper format, declare bad format and exit.
324 if ((up->rpt_buf[0] == (char) 0x41) ||
325 (up->rpt_buf[0] == (char) 0x46) ||
326 (up->rpt_buf[0] == (char) 0x54) ||
327 (up->rpt_buf[0] == (char) 0x4B) ||
328 (up->rpt_buf[0] == (char) 0x6D)) {
330 /* standard time packet - GPS time and GPS week number */
332 printf("Palisade Port B packets detected. Connect to Port A\n");
338 if (up->rpt_buf[0] == (char) 0x8f) {
342 event = (unsigned short) (getint((u_char *) &mb(1)) & 0xffff);
343 if (!((pp->sloppyclockflag & CLK_FLAG2) || event))
347 switch (mb(0) & 0xff) {
354 if (up->rpt_cnt != LENCODE_8F0B) /* check length */
360 double lat, lon, alt;
361 lat = getdbl((u_char *) &mb(42)) * R2D;
362 lon = getdbl((u_char *) &mb(50)) * R2D;
363 alt = getdbl((u_char *) &mb(58));
365 printf("TSIP_decode: unit %d: Latitude: %03.4f Longitude: %03.4f Alt: %05.2f m\n",
366 up->unit, lat,lon,alt);
367 printf("TSIP_decode: unit %d: Sats:", up->unit);
368 for (st = 66, ts = 0; st <= 73; st++) if (mb(st)) {
369 if (mb(st) > 0) ts++;
370 printf(" %02d", mb(st));
372 printf(" : Tracking %d\n", ts);
376 GPS_UTC_Offset = getint((u_char *) &mb(16));
377 if (GPS_UTC_Offset == 0) { /* Check UTC offset */
379 printf("TSIP_decode: UTC Offset Unknown\n");
384 secs = getdbl((u_char *) &mb(3));
385 secint = (long) secs;
386 secfrac = secs - secint; /* 0.0 <= secfrac < 1.0 */
388 pp->usec = (long) (secfrac * 1000000);
390 secint %= 86400; /* Only care about today */
391 pp->hour = secint / 3600;
393 pp->minute = secint / 60;
395 pp->second = secint % 60;
397 if ((pp->day = day_of_year(&mb(11))) < 0) break;
399 pp->year = getint((u_char *) &mb(13));
403 printf("TSIP_decode: unit %d: %02X #%d %02d:%02d:%02d.%06ld %02d/%02d/%04d UTC %02d\n",
404 up->unit, mb(0) & 0xff, event, pp->hour, pp->minute,
405 pp->second, pp->usec, mb(12), mb(11), pp->year, GPS_UTC_Offset);
407 /* Only use this packet when no
408 * 8F-AD's are being received
411 if (up->leap_status) {
420 /* Palisade-NTP Packet */
422 if (up->rpt_cnt != LENCODE_NTP) /* check length */
425 up->leap_status = mb(19);
430 /* Check Tracking Status */
432 if (st < 0 || st > 14) st = 14;
433 if ((st >= 2 && st <= 7) || st == 11 || st == 12) {
435 printf("TSIP_decode: Not Tracking Sats : %s\n",
436 *Tracking_Status[st]);
438 refclock_report(peer, CEVNT_BADTIME);
444 if (up->leap_status & PALISADE_LEAP_PENDING) {
445 if (up->leap_status & PALISADE_UTC_TIME)
446 pp->leap = LEAP_ADDSECOND;
448 pp->leap = LEAP_DELSECOND;
450 else if (up->leap_status)
451 pp->leap = LEAP_NOWARNING;
453 else { /* UTC flag is not set:
454 * Receiver may have been reset, and lost
455 * its UTC almanac data */
456 pp->leap = LEAP_NOTINSYNC;
458 printf("TSIP_decode: UTC Almanac unavailable: %d\n",
461 refclock_report(peer, CEVNT_BADTIME);
466 pp->usec = (long) (getdbl((u_char *) &mb(3)) * 1000000);
468 if ((pp->day = day_of_year(&mb(14))) < 0)
470 pp->year = getint((u_char *) &mb(16));
477 printf("TSIP_decode: unit %d: %02X #%d %02d:%02d:%02d.%06ld %02d/%02d/%04d UTC %02x %s\n",
478 up->unit, mb(0) & 0xff, event, pp->hour, pp->minute,
479 pp->second, pp->usec, mb(15), mb(14), pp->year,
480 mb(19), *Tracking_Status[st]);
491 refclock_report(peer, CEVNT_BADREPLY);
494 printf("TSIP_decode: unit %d: bad packet %02x-%02x event %d len %d\n",
495 up->unit, up->rpt_buf[0] & 0xff, mb(0) & 0xff,
502 * palisade__receive - receive data from the serial interface
516 struct palisade_unit *up;
517 struct refclockproc *pp;
520 * Initialize pointers and read the timecode and timestamp.
523 up = (struct palisade_unit *)pp->unitptr;
525 if (! TSIP_decode(peer)) return;
528 return; /* no poll pending, already received or timeout */
530 up->polled = 0; /* Poll reply received */
531 pp->lencode = 0; /* clear time code */
535 "palisade_receive: unit %d: %4d %03d %02d:%02d:%02d.%06ld\n",
536 up->unit, pp->year, pp->day, pp->hour, pp->minute,
537 pp->second, pp->usec);
542 * Generate timecode: YYYY DoY HH:MM:SS.microsec
546 (void) sprintf(pp->a_lastcode,"%4d %03d %02d:%02d:%02d.%06ld",
547 pp->year,pp->day,pp->hour,pp->minute, pp->second,pp->usec);
551 pp->lasttime = current_time;
553 if (!refclock_process(pp
555 , PALISADE_SAMPLES, PALISADE_SAMPLES * 3 / 5
558 refclock_report(peer, CEVNT_BADTIME);
561 printf("palisade_receive: unit %d: refclock_process failed!\n",
567 record_clock_stats(&peer->srcadr, pp->a_lastcode);
571 printf("palisade_receive: unit %d: %s\n",
572 up->unit, prettydate(&pp->lastrec));
575 refclock_receive(peer
577 , &pp->offset, 0, pp->dispersion,
578 &pp->lastrec, &pp->lastrec, pp->leap
585 * palisade_poll - called by the transmit procedure
601 struct palisade_unit *up;
602 struct refclockproc *pp;
605 up = (struct palisade_unit *)pp->unitptr;
608 if (up->polled > 0) /* last reply never arrived or error */
609 refclock_report(peer, CEVNT_TIMEOUT);
611 up->polled = 2; /* synchronous packet + 1 event */
615 printf("palisade_poll: unit %d: polling %s\n", unit,
616 (pp->sloppyclockflag & CLK_FLAG2) ?
617 "synchronous packet" : "event");
620 if (pp->sloppyclockflag & CLK_FLAG2)
621 return; /* using synchronous packet input */
624 refclock_report(peer, CEVNT_FAULT);
633 struct recvbuf *rbufp;
635 struct recvbuf *rbufp
640 * Initialize pointers and read the timecode and timestamp.
642 struct palisade_unit *up;
643 struct refclockproc *pp;
648 peer = (struct peer *)rbufp->recv_srcclock;
650 up = (struct palisade_unit *)pp->unitptr;
652 c = (char *) &rbufp->recv_space;
653 d = c + rbufp->recv_length;
657 /* Build time packet */
658 switch (up->rpt_status) {
660 case TSIP_PARSED_DLE_1:
666 up->rpt_status = TSIP_PARSED_EMPTY;
670 up->rpt_status = TSIP_PARSED_DATA;
677 case TSIP_PARSED_DATA:
679 up->rpt_status = TSIP_PARSED_DLE_2;
681 mb(up->rpt_cnt++) = *c;
684 case TSIP_PARSED_DLE_2:
686 up->rpt_status = TSIP_PARSED_DATA;
691 up->rpt_status = TSIP_PARSED_FULL;
693 /* error: start new report packet */
694 up->rpt_status = TSIP_PARSED_DLE_1;
699 case TSIP_PARSED_FULL:
700 case TSIP_PARSED_EMPTY:
703 up->rpt_status = TSIP_PARSED_EMPTY;
705 up->rpt_status = TSIP_PARSED_DLE_1;
711 if (up->rpt_status == TSIP_PARSED_DLE_1) {
713 if (pp->sloppyclockflag & CLK_FLAG2)
715 get_systime(&pp->lastrec);
717 else if (up->rpt_status == TSIP_PARSED_EMPTY)
720 else if (up->rpt_cnt > BMAX)
721 up->rpt_status =TSIP_PARSED_EMPTY;
723 if (up->rpt_status == TSIP_PARSED_FULL)
724 palisade_receive(peer);
726 } /* while chars in buffer */
731 * Trigger the Palisade's event input, which is driven off the RTS
733 * Take a system time stamp to match the GPS time stamp.
739 pp /* pointer to unit structure */
741 struct refclockproc * pp; /* pointer to unit structure */
743 struct refclockproc * pp /* pointer to unit structure */
747 int x; /* state before & after RTS set */
748 struct palisade_unit *up;
750 up = (struct palisade_unit *) pp->unitptr;
752 /* read the current status, so we put things back right */
753 if (ioctl(pp->io.fd, TIOCMGET, &x) < 0) {
756 printf("Palisade HW_poll: unit %d: GET %s\n", up->unit, strerror(errno));
758 msyslog(LOG_ERR, "Palisade(%d) HW_poll: ioctl(fd,GET): %m",
763 x |= TIOCM_RTS; /* turn on RTS */
766 if (ioctl(pp->io.fd, TIOCMSET, &x) < 0) {
769 printf("Palisade HW_poll: unit %d: SET \n", up->unit);
772 "Palisade(%d) HW_poll: ioctl(fd, SET, RTS_on): %m",
777 x &= ~TIOCM_RTS; /* turn off RTS */
780 get_systime(&pp->lastrec);
782 if (ioctl(pp->io.fd, TIOCMSET, &x) == -1) {
785 printf("Palisade HW_poll: unit %d: UNSET \n", up->unit);
788 "Palisade(%d) HW_poll: ioctl(fd, UNSET, RTS_off): %m",
798 * this 'casts' a character array into a float
812 #ifdef WORDS_BIGENDIAN
813 ((char *) &sval)[0] = *bp++;
814 ((char *) &sval)[1] = *bp++;
815 ((char *) &sval)[2] = *bp++;
816 ((char *) &sval)[3] = *bp++;
818 ((char *) &sval)[3] = *bp++;
819 ((char *) &sval)[2] = *bp++;
820 ((char *) &sval)[1] = *bp++;
821 ((char *) &sval)[0] = *bp;
822 #endif /* ! XNTP_BIG_ENDIAN */
828 * this 'casts' a character array into a double
842 #ifdef WORDS_BIGENDIAN
843 ((char *) &dval)[0] = *bp++;
844 ((char *) &dval)[1] = *bp++;
845 ((char *) &dval)[2] = *bp++;
846 ((char *) &dval)[3] = *bp++;
847 ((char *) &dval)[4] = *bp++;
848 ((char *) &dval)[5] = *bp++;
849 ((char *) &dval)[6] = *bp++;
850 ((char *) &dval)[7] = *bp;
852 ((char *) &dval)[7] = *bp++;
853 ((char *) &dval)[6] = *bp++;
854 ((char *) &dval)[5] = *bp++;
855 ((char *) &dval)[4] = *bp++;
856 ((char *) &dval)[3] = *bp++;
857 ((char *) &dval)[2] = *bp++;
858 ((char *) &dval)[1] = *bp++;
859 ((char *) &dval)[0] = *bp;
860 #endif /* ! XNTP_BIG_ENDIAN */
865 * cast a 16 bit character array into a short (16 bit) int
878 return (short) (bp[1] + (bp[0] << 8));
881 #endif /* REFCLOCK */