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