Home | History | Annotate | Line # | Download | only in libparse
clk_sel240x.c revision 1.2.8.2
      1  1.2.8.2  msaitoh /*	$NetBSD: clk_sel240x.c,v 1.2.8.2 2015/01/07 04:45:29 msaitoh Exp $	*/
      2  1.2.8.2  msaitoh 
      3  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
      4  1.2.8.2  msaitoh // Copyright (c) 2009,2012 -
      5  1.2.8.2  msaitoh //        Schweitzer Engineering Laboratories, Inc. <opensource (at) selinc.com>
      6  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
      7  1.2.8.2  msaitoh 
      8  1.2.8.2  msaitoh // Need to have _XOPEN_SOURCE defined for time.h to give the
      9  1.2.8.2  msaitoh // correct strptime signature.  As per feature_test_macros(7),
     10  1.2.8.2  msaitoh // define this before including any header files.
     11  1.2.8.2  msaitoh 
     12  1.2.8.2  msaitoh // #ifndef _XOPEN_SOURCE
     13  1.2.8.2  msaitoh // #define _XOPEN_SOURCE
     14  1.2.8.2  msaitoh // #endif
     15  1.2.8.2  msaitoh 
     16  1.2.8.2  msaitoh #ifdef HAVE_CONFIG_H
     17  1.2.8.2  msaitoh # include <config.h>
     18  1.2.8.2  msaitoh #endif
     19  1.2.8.2  msaitoh 
     20  1.2.8.2  msaitoh #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_SEL240X)
     21  1.2.8.2  msaitoh 
     22  1.2.8.2  msaitoh #include "ntp_syslog.h"
     23  1.2.8.2  msaitoh #include "ntp_types.h"
     24  1.2.8.2  msaitoh #include "ntp_fp.h"
     25  1.2.8.2  msaitoh #include "ntp_unixtime.h"
     26  1.2.8.2  msaitoh #include "ntp_calendar.h"
     27  1.2.8.2  msaitoh #include "ntp_machine.h"
     28  1.2.8.2  msaitoh #include "ntp_stdlib.h"
     29  1.2.8.2  msaitoh 
     30  1.2.8.2  msaitoh #include "parse.h"
     31  1.2.8.2  msaitoh 
     32  1.2.8.2  msaitoh #ifndef PARSESTREAM
     33  1.2.8.2  msaitoh # include <stdio.h>
     34  1.2.8.2  msaitoh #else
     35  1.2.8.2  msaitoh # include "sys/parsestreams.h"
     36  1.2.8.2  msaitoh #endif
     37  1.2.8.2  msaitoh 
     38  1.2.8.2  msaitoh #include <time.h>
     39  1.2.8.2  msaitoh 
     40  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
     41  1.2.8.2  msaitoh // The B8 output has the following format B8 = '\x01YYYY:ddd:hh:mm:ssq\r\n'
     42  1.2.8.2  msaitoh //    where q = ' ' locked
     43  1.2.8.2  msaitoh //              '.' <1 us
     44  1.2.8.2  msaitoh //              '*' <10 us
     45  1.2.8.2  msaitoh //              '#' <100 us
     46  1.2.8.2  msaitoh //              '?' >100 us
     47  1.2.8.2  msaitoh //
     48  1.2.8.2  msaitoh // Based on this we need to recored the stime when we receive the <SOH>
     49  1.2.8.2  msaitoh // character and end it when we see the \n.
     50  1.2.8.2  msaitoh //
     51  1.2.8.2  msaitoh // The q or quality character indicates satellite lock and sync.   For the
     52  1.2.8.2  msaitoh // purposes of NTP we are going to call it valid when we receive anything but
     53  1.2.8.2  msaitoh // a '?'.  But we are only going to call it synced when we receive a ' '
     54  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
     55  1.2.8.2  msaitoh 
     56  1.2.8.2  msaitoh static unsigned long inp_sel240x( parse_t *parseio,
     57  1.2.8.2  msaitoh 		                  unsigned int ch,
     58  1.2.8.2  msaitoh 				  timestamp_t *tstamp);
     59  1.2.8.2  msaitoh static unsigned long cvt_sel240x( unsigned char *buffer,
     60  1.2.8.2  msaitoh 		                  int size,
     61  1.2.8.2  msaitoh 				  struct format *format,
     62  1.2.8.2  msaitoh 				  clocktime_t *clock_time,
     63  1.2.8.2  msaitoh 				  void *local );
     64  1.2.8.2  msaitoh 
     65  1.2.8.2  msaitoh // Parse clock format structure describing the message above
     66  1.2.8.2  msaitoh static struct format sel240x_fmt =
     67  1.2.8.2  msaitoh { { {  6, 3 },
     68  1.2.8.2  msaitoh     {  0, 0 },
     69  1.2.8.2  msaitoh     {  1, 4 },
     70  1.2.8.2  msaitoh     { 10, 2 },
     71  1.2.8.2  msaitoh     { 13, 2 },
     72  1.2.8.2  msaitoh     { 16, 2 },
     73  1.2.8.2  msaitoh     {  0, 0 },
     74  1.2.8.2  msaitoh     {  0, 0 },
     75  1.2.8.2  msaitoh     {  0, 0 },
     76  1.2.8.2  msaitoh     {  0, 0 },
     77  1.2.8.2  msaitoh     {  0, 0 },
     78  1.2.8.2  msaitoh     {  0, 0 }
     79  1.2.8.2  msaitoh   },
     80  1.2.8.2  msaitoh   (const unsigned char *)"\x01    :   :  :  :   \x0d\x0a",
     81  1.2.8.2  msaitoh   0
     82  1.2.8.2  msaitoh };
     83  1.2.8.2  msaitoh 
     84  1.2.8.2  msaitoh // Structure desctibing the parser
     85  1.2.8.2  msaitoh clockformat_t clock_sel240x =
     86  1.2.8.2  msaitoh {
     87  1.2.8.2  msaitoh 	inp_sel240x,
     88  1.2.8.2  msaitoh 	cvt_sel240x,
     89  1.2.8.2  msaitoh 	pps_one,
     90  1.2.8.2  msaitoh 	(void*)&sel240x_fmt,
     91  1.2.8.2  msaitoh 	"SEL B8",
     92  1.2.8.2  msaitoh 	25,
     93  1.2.8.2  msaitoh 	0
     94  1.2.8.2  msaitoh };
     95  1.2.8.2  msaitoh 
     96  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
     97  1.2.8.2  msaitoh static unsigned long
     98  1.2.8.2  msaitoh inp_sel240x( parse_t      *parseio,
     99  1.2.8.2  msaitoh 	     unsigned int ch,
    100  1.2.8.2  msaitoh 	     timestamp_t  *tstamp
    101  1.2.8.2  msaitoh 	   )
    102  1.2.8.2  msaitoh {
    103  1.2.8.2  msaitoh 	unsigned long rc;
    104  1.2.8.2  msaitoh 
    105  1.2.8.2  msaitoh 	parseprintf( DD_PARSE,
    106  1.2.8.2  msaitoh 	             ("inp_sel240x(0x%lx, 0x%x, ...)\n",(long)parseio, ch));
    107  1.2.8.2  msaitoh 
    108  1.2.8.2  msaitoh 	switch( ch )
    109  1.2.8.2  msaitoh 	{
    110  1.2.8.2  msaitoh 	case '\x01':
    111  1.2.8.2  msaitoh 		parseio->parse_index = 1;
    112  1.2.8.2  msaitoh 		parseio->parse_data[0] = ch;
    113  1.2.8.2  msaitoh 		parseio->parse_dtime.parse_stime = *tstamp;
    114  1.2.8.2  msaitoh 		rc = PARSE_INP_SKIP;
    115  1.2.8.2  msaitoh 		break;
    116  1.2.8.2  msaitoh 	case '\n':
    117  1.2.8.2  msaitoh 		if( (rc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP )
    118  1.2.8.2  msaitoh 		{
    119  1.2.8.2  msaitoh 			rc = parse_end( parseio );
    120  1.2.8.2  msaitoh 		}
    121  1.2.8.2  msaitoh 		break;
    122  1.2.8.2  msaitoh 	default:
    123  1.2.8.2  msaitoh 		rc = parse_addchar( parseio, ch );
    124  1.2.8.2  msaitoh 	}
    125  1.2.8.2  msaitoh 
    126  1.2.8.2  msaitoh 	return rc;
    127  1.2.8.2  msaitoh }
    128  1.2.8.2  msaitoh 
    129  1.2.8.2  msaitoh //////////////////////////////////////////////////////////////////////////////
    130  1.2.8.2  msaitoh static unsigned long
    131  1.2.8.2  msaitoh cvt_sel240x( unsigned char *buffer,
    132  1.2.8.2  msaitoh 	     int            size,
    133  1.2.8.2  msaitoh 	     struct format *format,
    134  1.2.8.2  msaitoh 	     clocktime_t   *clock_time,
    135  1.2.8.2  msaitoh 	     void          *local
    136  1.2.8.2  msaitoh 	   )
    137  1.2.8.2  msaitoh {
    138  1.2.8.2  msaitoh 	unsigned long rc = CVT_NONE;
    139  1.2.8.2  msaitoh 
    140  1.2.8.2  msaitoh 	if( Strok(buffer, format->fixed_string) )
    141  1.2.8.2  msaitoh 	{
    142  1.2.8.2  msaitoh 		struct tm ptime;
    143  1.2.8.2  msaitoh 		buffer++;
    144  1.2.8.2  msaitoh 		buffer = (unsigned char *) strptime(
    145  1.2.8.2  msaitoh 			(const char *)buffer, "%Y:%j:%H:%M:%S", &ptime );
    146  1.2.8.2  msaitoh 		if( *(buffer+1) != '\x0d' )
    147  1.2.8.2  msaitoh 		{
    148  1.2.8.2  msaitoh 			rc = CVT_FAIL | CVT_BADFMT;
    149  1.2.8.2  msaitoh 		}
    150  1.2.8.2  msaitoh 		else
    151  1.2.8.2  msaitoh 		{
    152  1.2.8.2  msaitoh 			clock_time->day = ptime.tm_mday;
    153  1.2.8.2  msaitoh 			clock_time->month = ptime.tm_mon + 1;
    154  1.2.8.2  msaitoh 			clock_time->year = ptime.tm_year + 1900;
    155  1.2.8.2  msaitoh 			clock_time->hour = ptime.tm_hour;
    156  1.2.8.2  msaitoh 			clock_time->minute = ptime.tm_min;
    157  1.2.8.2  msaitoh 			clock_time->second = ptime.tm_sec;
    158  1.2.8.2  msaitoh 			clock_time->usecond = 0;
    159  1.2.8.2  msaitoh 			clock_time->utcoffset = 0;
    160  1.2.8.2  msaitoh 			clock_time->flags = PARSEB_UTC;
    161  1.2.8.2  msaitoh 
    162  1.2.8.2  msaitoh 			if( *buffer == '?' )
    163  1.2.8.2  msaitoh 			{
    164  1.2.8.2  msaitoh 				clock_time->flags |= PARSEB_POWERUP;
    165  1.2.8.2  msaitoh 			}
    166  1.2.8.2  msaitoh 			else if( *buffer != ' ' )
    167  1.2.8.2  msaitoh 			{
    168  1.2.8.2  msaitoh 				clock_time->flags |= PARSEB_NOSYNC;
    169  1.2.8.2  msaitoh 			}
    170  1.2.8.2  msaitoh 
    171  1.2.8.2  msaitoh 			rc = CVT_OK;
    172  1.2.8.2  msaitoh 		}
    173  1.2.8.2  msaitoh 	}
    174  1.2.8.2  msaitoh 
    175  1.2.8.2  msaitoh 	return rc;
    176  1.2.8.2  msaitoh }
    177  1.2.8.2  msaitoh 
    178  1.2.8.2  msaitoh #else  /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SEL240X) */
    179  1.2.8.2  msaitoh int clk_sel240x_bs;
    180  1.2.8.2  msaitoh #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SEL240X) */
    181