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