Home | History | Annotate | Line # | Download | only in libparse
clk_sel240x.c revision 1.1.1.1.6.2
      1  1.1.1.1.6.2       snj /*	$NetBSD: clk_sel240x.c,v 1.1.1.1.6.2 2015/04/23 18:53:02 snj 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.1.1.6.2       snj // 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.1.1.6.2       snj static parse_inp_fnc_t inp_sel240x;
     57  1.1.1.1.6.2       snj static parse_cvt_fnc_t cvt_sel240x;
     58          1.1  christos 
     59          1.1  christos // Parse clock format structure describing the message above
     60          1.1  christos static struct format sel240x_fmt =
     61          1.1  christos { { {  6, 3 },
     62          1.1  christos     {  0, 0 },
     63          1.1  christos     {  1, 4 },
     64          1.1  christos     { 10, 2 },
     65          1.1  christos     { 13, 2 },
     66          1.1  christos     { 16, 2 },
     67          1.1  christos     {  0, 0 },
     68          1.1  christos     {  0, 0 },
     69          1.1  christos     {  0, 0 },
     70          1.1  christos     {  0, 0 },
     71          1.1  christos     {  0, 0 },
     72          1.1  christos     {  0, 0 }
     73          1.1  christos   },
     74          1.1  christos   (const unsigned char *)"\x01    :   :  :  :   \x0d\x0a",
     75          1.1  christos   0
     76          1.1  christos };
     77          1.1  christos 
     78          1.1  christos // Structure desctibing the parser
     79          1.1  christos clockformat_t clock_sel240x =
     80          1.1  christos {
     81          1.1  christos 	inp_sel240x,
     82          1.1  christos 	cvt_sel240x,
     83          1.1  christos 	pps_one,
     84          1.1  christos 	(void*)&sel240x_fmt,
     85          1.1  christos 	"SEL B8",
     86          1.1  christos 	25,
     87          1.1  christos 	0
     88          1.1  christos };
     89          1.1  christos 
     90          1.1  christos //////////////////////////////////////////////////////////////////////////////
     91          1.1  christos static unsigned long
     92          1.1  christos inp_sel240x( parse_t      *parseio,
     93  1.1.1.1.6.2       snj 	     char         ch,
     94          1.1  christos 	     timestamp_t  *tstamp
     95          1.1  christos 	   )
     96          1.1  christos {
     97          1.1  christos 	unsigned long rc;
     98          1.1  christos 
     99          1.1  christos 	parseprintf( DD_PARSE,
    100          1.1  christos 	             ("inp_sel240x(0x%lx, 0x%x, ...)\n",(long)parseio, ch));
    101          1.1  christos 
    102          1.1  christos 	switch( ch )
    103          1.1  christos 	{
    104          1.1  christos 	case '\x01':
    105          1.1  christos 		parseio->parse_index = 1;
    106          1.1  christos 		parseio->parse_data[0] = ch;
    107          1.1  christos 		parseio->parse_dtime.parse_stime = *tstamp;
    108          1.1  christos 		rc = PARSE_INP_SKIP;
    109          1.1  christos 		break;
    110          1.1  christos 	case '\n':
    111          1.1  christos 		if( (rc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP )
    112          1.1  christos 		{
    113          1.1  christos 			rc = parse_end( parseio );
    114          1.1  christos 		}
    115          1.1  christos 		break;
    116          1.1  christos 	default:
    117          1.1  christos 		rc = parse_addchar( parseio, ch );
    118          1.1  christos 	}
    119          1.1  christos 
    120          1.1  christos 	return rc;
    121          1.1  christos }
    122          1.1  christos 
    123          1.1  christos //////////////////////////////////////////////////////////////////////////////
    124          1.1  christos static unsigned long
    125          1.1  christos cvt_sel240x( unsigned char *buffer,
    126          1.1  christos 	     int            size,
    127          1.1  christos 	     struct format *format,
    128          1.1  christos 	     clocktime_t   *clock_time,
    129          1.1  christos 	     void          *local
    130          1.1  christos 	   )
    131          1.1  christos {
    132          1.1  christos 	unsigned long rc = CVT_NONE;
    133          1.1  christos 
    134          1.1  christos 	if( Strok(buffer, format->fixed_string) )
    135          1.1  christos 	{
    136          1.1  christos 		struct tm ptime;
    137          1.1  christos 		buffer++;
    138  1.1.1.1.6.2       snj 		buffer = (unsigned char *) strptime(
    139          1.1  christos 			(const char *)buffer, "%Y:%j:%H:%M:%S", &ptime );
    140          1.1  christos 		if( *(buffer+1) != '\x0d' )
    141          1.1  christos 		{
    142          1.1  christos 			rc = CVT_FAIL | CVT_BADFMT;
    143          1.1  christos 		}
    144          1.1  christos 		else
    145          1.1  christos 		{
    146          1.1  christos 			clock_time->day = ptime.tm_mday;
    147          1.1  christos 			clock_time->month = ptime.tm_mon + 1;
    148          1.1  christos 			clock_time->year = ptime.tm_year + 1900;
    149          1.1  christos 			clock_time->hour = ptime.tm_hour;
    150          1.1  christos 			clock_time->minute = ptime.tm_min;
    151          1.1  christos 			clock_time->second = ptime.tm_sec;
    152          1.1  christos 			clock_time->usecond = 0;
    153          1.1  christos 			clock_time->utcoffset = 0;
    154          1.1  christos 			clock_time->flags = PARSEB_UTC;
    155          1.1  christos 
    156          1.1  christos 			if( *buffer == '?' )
    157          1.1  christos 			{
    158          1.1  christos 				clock_time->flags |= PARSEB_POWERUP;
    159          1.1  christos 			}
    160          1.1  christos 			else if( *buffer != ' ' )
    161          1.1  christos 			{
    162          1.1  christos 				clock_time->flags |= PARSEB_NOSYNC;
    163          1.1  christos 			}
    164          1.1  christos 
    165          1.1  christos 			rc = CVT_OK;
    166          1.1  christos 		}
    167          1.1  christos 	}
    168          1.1  christos 
    169          1.1  christos 	return rc;
    170          1.1  christos }
    171          1.1  christos 
    172          1.1  christos #else  /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SEL240X) */
    173          1.1  christos int clk_sel240x_bs;
    174          1.1  christos #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SEL240X) */
    175