[LinuxPPS] refclock_nmea patch

Udo van den Heuvel udovdh at xs4all.nl
Tue Oct 23 05:54:43 CEST 2007


Rodolfo Giometti wrote:
>>>>> Oct 22 19:19:53 epia ntpd[3499]: refclock_nmea: try alternate PPS device
>>>>> /dev/pps0
>>>>> Oct 22 19:19:53 epia ntpd[3499]: refclock_nmea: found PPS source
>>>>> "/dev/pps%d" at id #6 on ""
>>>>>
>>>>> Note the %d in the message string and that the first gps0 device is not
>>>>> used. 
>>>> /sys/class/pps has a directory for pps0 but not for gps0.
>>> This directory hold __only__ PPS devices.
>> The first pps device I offer is via /dev/gps0 and it is not used.
> 
> With "used" do you mean "used by ntpd as PPS source" or "used by ntpd
> as GPS data source"?

You mean NMEA when you say 'GPS data source'?
I think both, looking at the code. (where the PPS API interface is started)

> The former is correct beacuse /dev/gps0 is _not_ a PPS source, it's a
> serial line (points to /dev/ttyS0), the latter I don't know. :)

We open a fd on a device, do time_pps_create and when it fails, we open
the fd on another device, do another time_pps_create...
So the fd is not saved.
So how can ntpd use the first device for NMEA and the second for PPS?

Maybe this can help:
When is the /dev entry (pps0) created?
Based on what input?

-------------- next part --------------
--- org.ntp-4.2.4p2/ntpd/refclock_nmea.c	2006-06-06 22:16:53.000000000 +0200
+++ ntp-4.2.4p2/ntpd/refclock_nmea.c	2007-10-23 05:52:09.000000000 +0200
@@ -61,6 +61,7 @@
 # define DEVICE "COM%d:" 	/* COM 1 - 3 supported */
 #else
 # define DEVICE	"/dev/gps%d"	/* name of radio device */
+# define DEVICEPPS "/dev/pps%d" /* name of alternate PPS radio device */
 #endif
 #define	SPEED232	B4800	/* uart speed (4800 bps) */
 #define	PRECISION	(-9)	/* precision assumed (about 2 ms) */
@@ -71,6 +72,7 @@
 #define RANGEGATE	500000	/* range gate (ns) */
 
 #define LENNMEA		75	/* min timecode length */
+#define LENPPS		PPS_MAX_NAME_LEN
 
 /*
  * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
@@ -231,15 +233,27 @@
 	 * the assert edge and do not enable the kernel hardpps.
 	 */
 	if (time_pps_create(fd, &up->handle) < 0) {
-		up->handle = 0;
-		msyslog(LOG_ERR,
-		    "refclock_nmea: time_pps_create failed: %m");
-		return (1);
+		/* Try the alternate PPS device */
+                (void) sprintf(device, DEVICEPPS, unit);
+		msyslog(LOG_ERR, "refclock_nmea: try alternate PPS device \"%s\"", device);
+                fd = open(device, O_RDWR);
+                if (fd < 0)
+                	goto pps_error;
+		if (time_pps_create(fd, &up->handle) < 0)
+               		goto pps_error;
 	}
+	else (void) sprintf(device, DEVICE, unit); /* just rebuild device's name */
+	msyslog(LOG_INFO, "refclock_nmea: found PPS source \"%s\"", device);
 	return(nmea_ppsapi(peer, 0, 0));
 #else
 	return (1);
 #endif /* HAVE_PPSAPI */
+
+pps_error:
+	/* No luck, no PPS unit available! */
+	up->handle = -1;
+	msyslog(LOG_ERR, "refclock_nmea: no PPS devices found at " DEVICE " nor " DEVICEPPS ": %m", unit, unit);
+	return (1);
 }
 
 /*
@@ -257,8 +271,7 @@
 	pp = peer->procptr;
 	up = (struct nmeaunit *)pp->unitptr;
 #ifdef HAVE_PPSAPI
-	if (up->handle != 0)
-		time_pps_destroy(up->handle);
+	time_pps_destroy(up->handle);
 #endif /* HAVE_PPSAPI */
 	io_closeclock(&pp->io);
 	free(up);
@@ -366,7 +379,7 @@
 	/*
 	 * Convert the timespec nanoseconds field to ntp l_fp units.
 	 */ 
-	if (up->handle == 0)
+	if (up->handle == -1)
 		return (0);
 	timeout.tv_sec = 0;
 	timeout.tv_nsec = 0;


More information about the LinuxPPS mailing list