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