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