[LinuxPPS] refclock_nmea patch

Rodolfo Giometti giometti at enneenne.com
Mon Oct 22 12:42:07 CEST 2007


On Sun, Oct 21, 2007 at 02:32:22PM +0200, Udo van den Heuvel wrote:
> Udo van den Heuvel wrote:
> > Here's the same patch, but against the bare ntp source.
> 
> An update, still wrestling with the #defines.
> Compiles more or less, needs review.

> --- refclock_nmea.c.orig	2007-10-21 14:00:49.000000000 +0200
> +++ refclock_nmea.c	2007-10-21 14:00:53.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 PPS radio device */

Better write: "name of alternate PPS radio device", since your code
should check for PPS support into DEVICE first and then, in case of
fault, into DEVICEPPS.

>  #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
> @@ -91,6 +93,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 +142,11 @@
>  	register struct nmeaunit *up;
>  	struct refclockproc *pp;
>  	int fd;
> +#ifdef HAVE_PPSAPI  /* PPS_HAVE_FINDPATH */
> +	char id[LENPPS] = "",
> +	     path[LENPPS],
> +	     link[LENPPS] = "/dev/gps0";    /* just a default device */
> +#endif	/* PPS_HAVE_FINDPATH */

These are not neede anymore.

>  	char device[20];
>  
>  	/*
> @@ -231,15 +239,36 @@
>  	 * the assert edge and do not enable the kernel hardpps.
>  	 */
>  	if (time_pps_create(fd, &up->handle) < 0) {
> +#ifndef PPS_HAVE_FINDPATH

Remove this #ifndef, is not needed!

>  		up->handle = 0;
>  		msyslog(LOG_ERR,
>  		    "refclock_nmea: time_pps_create failed: %m");
>  		return (1);
> +#else

Remove it! You should check for the alternate PPS device _each time_
main device fail.

> +		/* Try the alternare PPS device */
> +                (void) sprintf(device, DEVICEPPS, unit);
> +		msyslog(LOG_ERR, "refclock_nmea: try alternate PPS device %s", device);
> +                fd = open(device, O_RDWR);

Remoce the open, just use the time_pps_create() checking the return
value.

> +                if (fd < 0)
> +                	goto pps_error;
> +                  
> +		if (time_pps_create(fd, &up->handle) < 0)
> +               		goto pps_error;
> +#endif /* PPS_HAVE_FINDPATH */
>  	}
> +	msyslog(LOG_INFO, "refclock_nmea: found PPS source \"%s\" at id #%d on \"%s\"", path, fd, id);
> +	up->handle_created = ~0;
>  	return(nmea_ppsapi(peer, 0, 0));
> -#else
> +#else /* HAVE_PPSAPI */
>  	return (1);
>  #endif /* HAVE_PPSAPI */
> +
> +pps_error:
> +	/* No luck, no PPS unit available! */
> +	up->handle = 0;
> +	up->handle_created = 0;
> +	msyslog(LOG_ERR, "refclock_nmea: alternate PPS device %s fail : %m", device);
> +	return (1);
>  }
>  
>  /*
> @@ -257,8 +286,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;

Also here you can call time_pps_destroy() without worry about
up->handle_created value (so you can safely remove this variable and
the if clause).

> +	}
>  #endif /* HAVE_PPSAPI */
>  	io_closeclock(&pp->io);
>  	free(up);
> @@ -366,7 +397,7 @@
>  	/*
>  	 * Convert the timespec nanoseconds field to ntp l_fp units.
>  	 */ 
> -	if (up->handle == 0)
> +	if (!up->handle_created)

Maybe you can just use up->handle, but I suggest to set it to "-1" in
case of "no PPS" sources.

>  		return (0);
>  	timeout.tv_sec = 0;
>  	timeout.tv_nsec = 0;

Ciao,

Rodolfo

-- 

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



More information about the LinuxPPS mailing list