2 * refclock_fg - clock driver for the Forum Graphic GPS datating station
9 #if defined(REFCLOCK) && defined(CLOCK_FG)
13 #include "ntp_refclock.h"
14 #include "ntp_calendar.h"
15 #include "ntp_stdlib.h"
18 * This driver supports the Forum Graphic GPS dating station.
19 * More information about FG GPS is available on http://www.forumgraphic.com
20 * Contact das@amt.ru for any question about this driver.
24 * Interface definitions
26 #define DEVICE "/dev/fgclock%d"
27 #define PRECISION (-10) /* precision assumed (about 1 ms) */
29 #define DESCRIPTION "Forum Graphic GPS dating station"
30 #define LENFG 26 /* timecode length */
31 #define SPEED232 B9600 /* uart speed (9600 baud) */
36 static int fg_init (int);
37 static int fg_start (int, struct peer *);
38 static void fg_shutdown (int, struct peer *);
39 static void fg_poll (int, struct peer *);
40 static void fg_receive (struct recvbuf *);
43 * Forum Graphic unit control structure
47 int pollnum; /* Use peer.poll instead? */
48 int status; /* Hug to check status information on GPS */
49 int y2kwarn; /* Y2K bug */
55 static char fginit[] = { 0x10, 0x48, 0x10, 0x0D, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0 };
57 static char fgdate[] = { 0x10, 0x44, 0x10, 0x0D, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0 };
63 struct refclock refclock_fg = {
64 fg_start, /* start up driver */
65 fg_shutdown, /* shut down driver */
66 fg_poll, /* transmit poll message */
67 noentry, /* not used */
68 noentry, /* initialize driver (not used) */
69 noentry, /* not used */
70 NOFLAGS /* not used */
74 * fg_init - Initialization of FG GPS.
82 if (write(fd, fginit, LENFG) != LENFG)
89 * fg_start - open the device and initialize data for processing
97 struct refclockproc *pp;
104 * Open device file for reading.
106 snprintf(device, sizeof(device), DEVICE, unit);
108 DPRINTF(1, ("starting FG with device %s\n",device));
110 fd = refclock_open(device, SPEED232, LDISC_CLK);
115 * Allocate and initialize unit structure
118 up = emalloc(sizeof(struct fgunit));
119 memset(up, 0, sizeof(struct fgunit));
122 pp->io.clock_recv = fg_receive;
123 pp->io.srcclock = peer;
126 if (!io_addclock(&pp->io)) {
134 * Initialize miscellaneous variables
136 peer->precision = PRECISION;
137 pp->clockdesc = DESCRIPTION;
138 memcpy(&pp->refid, REFID, 3);
142 * Setup dating station to use GPS receiver.
143 * GPS receiver should work before this operation.
145 if(!fg_init(pp->io.fd))
146 refclock_report(peer, CEVNT_FAULT);
153 * fg_shutdown - shut down the clock
161 struct refclockproc *pp;
167 io_closeclock(&pp->io);
174 * fg_poll - called by the transmit procedure
182 struct refclockproc *pp;
187 * Time to poll the clock. The FG clock responds to a
188 * "<DLE>D<DLE><CR>" by returning a timecode in the format specified
189 * above. If nothing is heard from the clock for two polls,
190 * declare a timeout and keep going.
193 if (write(pp->io.fd, fgdate, LENFG) != LENFG)
194 refclock_report(peer, CEVNT_FAULT);
199 if (pp->coderecv == pp->codeproc) {
200 refclock_report(peer, CEVNT_TIMEOUT);
205 record_clock_stats(&peer->srcadr, pp->a_lastcode);
212 * fg_receive - receive data from the serial interface
216 struct recvbuf *rbufp
219 struct refclockproc *pp;
225 * Initialize pointers and read the timecode and timestamp
226 * We can't use gtlin function because we need bynary data in buf */
228 peer = rbufp->recv_peer;
233 * Below hug to implement receiving of status information
241 if (rbufp->recv_length < (LENFG - 2)) {
242 refclock_report(peer, CEVNT_BADREPLY);
243 return; /* The reply is invalid discard it. */
246 /* Below I trying to find a correct reply in buffer.
247 * Sometime GPS reply located in the beginnig of buffer,
248 * sometime you can find it with some offset.
251 bpt = (char *)rbufp->recv_space.X_recv_buffer;
252 while (*bpt != '\x10')
255 #define BP2(x) ( bpt[x] & 15 )
256 #define BP1(x) (( bpt[x] & 240 ) >> 4)
258 pp->year = BP1(2) * 10 + BP2(2);
260 if (pp->year == 94) {
261 refclock_report(peer, CEVNT_BADREPLY);
262 if (!fg_init(pp->io.fd))
263 refclock_report(peer, CEVNT_FAULT);
265 /* GPS is just powered up. The date is invalid -
266 discarding it. Initilize GPS one more time */
267 /* Sorry - this driver will broken in 2094 ;) */
274 pp->day = 100 * BP2(3) + 10 * BP1(4) + BP2(4);
277 After Jan, 10 2000 Forum Graphic GPS receiver had a very strange
278 benahour. It doubles day number for an hours in replys after 10:10:10 UTC
279 and doubles min every hour at HH:10:ss for a minute.
280 Hope it is a problem of my unit only and not a Y2K problem of FG GPS.
281 Below small code to avoid such situation.
283 if (up->y2kwarn > 10)
284 pp->hour = BP1(6)*10 + BP2(6);
286 pp->hour = BP1(5)*10 + BP2(5);
288 if ((up->y2kwarn > 10) && (pp->hour == 10)) {
289 pp->minute = BP1(7)*10 + BP2(7);
290 pp->second = BP1(8)*10 + BP2(8);
291 pp->nsec = (BP1(9)*10 + BP2(9)) * 1000000;
292 pp->nsec += BP1(10) * 1000;
294 pp->hour = BP1(5)*10 + BP2(5);
295 pp->minute = BP1(6)*10 + BP2(6);
296 pp->second = BP1(7)*10 + BP2(7);
297 pp->nsec = (BP1(8)*10 + BP2(8)) * 1000000;
298 pp->nsec += BP1(9) * 1000;
301 if ((pp->hour == 10) && (pp->minute == 10)) {
305 snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
306 "%d %d %d %d %d", pp->year, pp->day, pp->hour,
307 pp->minute, pp->second);
308 pp->lencode = strlen(pp->a_lastcode);
309 /*get_systime(&pp->lastrec);*/
313 printf("fg: time is %04d/%03d %02d:%02d:%02d UTC\n",
314 pp->year, pp->day, pp->hour, pp->minute, pp->second);
317 pp->lastrec = rbufp->recv_time; /* Is it better than get_systime()? */
318 /* pp->leap = LEAP_NOWARNING; */
321 * Process the new sample in the median filter and determine the
322 * timecode timestamp.
325 if (!refclock_process(pp))
326 refclock_report(peer, CEVNT_BADTIME);
327 pp->lastref = pp->lastrec;
328 refclock_receive(peer);
335 #endif /* REFCLOCK */