[LinuxPPS] [PATCH] time_pps_readlink()

Rodolfo Giometti giometti at linux.it
Wed Aug 30 15:18:29 CEST 2006


On Sun, Aug 27, 2006 at 09:26:34AM +0200, Udo van den Heuvel wrote:
> The new NMEA patch appears to work OK.
> I attached a small update to the patch which reduces the need for
> LINUXPPS_MAX_NAME_LEN a bit. Please feel free to post the patch online.
> 
> If someone can explain Rodolfo's idea to make the patch even smaller,
> please post!

Here Rodolfo's idea! ;)

As you can see I used less ifdef lines and the patch is more readable.

Please, let me know what do you think about it.

Ciao,

Rodolfo

P.S. The patch should be tested since I haven't any NMEA device.

-- 

GNU/Linux Solutions                  e-mail:    giometti at enneenne.com
Linux Device Driver                             giometti at gnudd.com
Embedded Systems                     		giometti at linux.it
UNIX programming                     phone:     +39 349 2432127
-------------- next part --------------
--- ntpd/refclock_nmea.c.orig	2006-08-30 15:19:51.000000000 +0200
+++ ntpd/refclock_nmea.c	2006-08-30 15:29:33.000000000 +0200
@@ -71,6 +71,7 @@
 #define RANGEGATE	500000	/* range gate (ns) */
 
 #define LENNMEA		75	/* min timecode length */
+#define LENPPS		LINUXPPS_MAX_NAME_LEN
 
 /*
  * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
@@ -91,6 +92,7 @@
 	pps_params_t pps_params; /* pps parameters */
 	pps_info_t pps_info;	/* last pps data */
 	pps_handle_t handle;	/* pps handlebars */
+	int handle_created;	/* pps handle created flag */
 #endif /* HAVE_PPSAPI */
 };
 
@@ -139,6 +141,11 @@
 	register struct nmeaunit *up;
 	struct refclockproc *pp;
 	int fd;
+#ifdef PPS_HAVE_FINDPATH
+	char id[LENPPS] = "",
+	     path[LENPPS],
+	     link[LENPPS] = "/dev/gps0";    /* just a default device */
+#endif	/* PPS_HAVE_FINDPATH */
 	char device[20];
 
 	/*
@@ -230,12 +237,25 @@
 	 * Start the PPSAPI interface if it is there. Default to use
 	 * the assert edge and do not enable the kernel hardpps.
 	 */
+#ifdef PPS_HAVE_FINDPATH
+	/* Get the PPS source's real name */
+	time_pps_readlink(link, LENPPS, path, LENPPS);
+
+	/* Try to find the source */
+	fd = time_pps_findpath(path, LENPPS, id, LENPPS);
+	if (fd < 0) {
+		msyslog(LOG_ERR, "refclock: cannot find PPS path \"%s\" in the system", path);
+		return 1;
+	}
+	msyslog(LOG_INFO, "refclock: found PPS source \"%s\" at id #%d on \"%s\"", path, fd, id);
+#endif	/* PPS_HAVE_FINDPATH */
 	if (time_pps_create(fd, &up->handle) < 0) {
-		up->handle = 0;
+		up->handle_created = 0;
 		msyslog(LOG_ERR,
 		    "refclock_nmea: time_pps_create failed: %m");
 		return (1);
 	}
+	up->handle_created = ~0;
 	return(nmea_ppsapi(peer, 0, 0));
 #else
 	return (1);
@@ -257,8 +277,10 @@
 	pp = peer->procptr;
 	up = (struct nmeaunit *)pp->unitptr;
 #ifdef HAVE_PPSAPI
-	if (up->handle != 0)
+	if (up->handle_created) {
 		time_pps_destroy(up->handle);
+		up->handle_created = 0;
+	}
 #endif /* HAVE_PPSAPI */
 	io_closeclock(&pp->io);
 	free(up);
@@ -366,7 +388,7 @@
 	/*
 	 * Convert the timespec nanoseconds field to ntp l_fp units.
 	 */ 
-	if (up->handle == 0)
+	if (!up->handle_created)
 		return (0);
 	timeout.tv_sec = 0;
 	timeout.tv_nsec = 0;


More information about the LinuxPPS mailing list