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