xref: /freebsd/contrib/ntp/html/parsenew.html (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3<html>
4
5	<head>
6		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
7		<title>Making PARSE Clocks</title>
8		<link href="scripts/style.css" type="text/css" rel="stylesheet">
9	</head>
10
11	<body>
12		<h3>How to build new PARSE clocks</h3>
13		<p>Here is an attempt to sketch out what you need to do in order to add another clock to the parse driver: Currently the implementation is being cleaned up - so not all information in here is completely correct. Refer to the included code where in doubt.</p>
14<p>Last update:
15  <!-- #BeginDate format:En2m -->13-Oct-2010  00:33<!-- #EndDate -->
16    UTC</p>
17		<p>Prerequisites:</p>
18		<ul>
19			<li>Does the system you want the clock connect to have the include files termio.h or termios.h ? (You need that for the parse driver)
20		</ul>
21		<p>What to do:</p>
22		<p>Make a conversion module (libparse/clk_*.c)</p>
23		<ol>
24			<li>What ist the time code format ?
25				<ul>
26					<li>find year, month, day, hour, minute, second, status (synchronised or not), possibly time zone information (you need to give the offset to UTC) You will have to convert the data from a string into a struct clocktime:
27						<pre>
28      struct clocktime                /* clock time broken up from time code */
29      {
30	long day;
31	long month;
32	long year;
33	long hour;
34	long minute;
35	long second;
36	long usecond;
37	long utcoffset;       /* in seconds */
38	time_t utcoffset;     /* true utc time instead of date/time */
39	long flags;           /* current clock status */
40      };
41</pre>
42						<p>Conversion is usually simple and straight forward. For the flags following values can be OR'ed together:</p>
43						<pre>
44     PARSEB_ANNOUNCE           switch time zone warning (informational only)
45     PARSEB_POWERUP            no synchronisation - clock confused (must set then)
46     PARSEB_NOSYNC             timecode currently not confirmed (must set then)
47                               usually on reception error when there is still a
48                               chance the the generated time is still ok.
49
50     PARSEB_DST                DST in effect (informational only)
51     PARSEB_UTC                timecode contains UTC time (informational only)
52     PARSEB_LEAPADD            LEAP addition warning (prior to leap happening - must set when imminent)
53			       also used for time code that do not encode the
54			       direction (as this is currently the default).
55     PARSEB_LEAPDEL            LEAP deletion warning (prior to leap happening - must set when imminent)
56     PARSEB_ALTERNATE          backup transmitter (informational only)
57     PARSEB_POSITION           geographic position available (informational only)
58     PARSEB_LEAPSECOND         actual leap second (this time code is the leap
59                               second - informational only)
60</pre>
61						<p>These are feature flags denoting items that are supported by the clock:</p>
62						<pre>
63     PARSEB_S_LEAP             supports LEAP - might set PARSEB_LEAP
64     PARSEB_S_ANTENNA          supports ANTENNA - might set PARSEB_ALTERNATE
65     PARSEB_S_PPS              supports PPS time stamping
66     PARSEB_S_POSITION         supports position information (GPS)
67   </pre>
68						<p>If the utctime field is non zero this value will be take as time code value. This allows for conversion routines that already have the utc time value. The utctime field gives the seconds since Jan 1st 1970, 0:00:00. The useconds field gives the respective usec value. The fields for date and time (down to second resolution) will be ignored.</p>
69						<p>Conversion is done in the cvt_* routine in parse/clk_*.c files. look in them for examples. The basic structure is:</p>
70						<pre>
71     struct clockformat &lt;yourclock&gt;_format = {
72       lots of fields for you to fill out (see below)
73     };
74
75     static cvt_&lt;yourclock&gt;()
76       ...
77     {
78       if (&lt;I do not recognize my time code&gt;) {
79         return CVT_NONE;
80       } else {
81         if (&lt;conversion into clockformat is ok&gt;) {
82           &lt;set all necessary flags&gt;;
83           return CVT_OK;
84         } else {
85           return CVT_FAIL|CVT_BADFMT;
86         }
87       }
88</pre>
89						<p>The struct clockformat is the interface to the rest of the parse driver - it holds all information necessary for finding the clock message and doing the appropriate time stamping.</p>
90						<pre>
91struct clockformat
92{
93  u_long (*input)();
94  /* input routine - your routine - cvt_&lt;yourclock&gt; */
95  u_long (*convert)();
96  /* conversion routine - your routine - cvt_&lt;yourclock&gt; */
97  /* routine for handling RS232 sync events (time stamps) - usually sync_simple */
98  u_long (*syncpps)();
99  /* PPS input routine - usually pps_one */
100  void           *data;
101  /* local parameters - any parameters/data/configuration info your conversion
102     routine might need */
103  char           *name;
104  /* clock format name - Name of the time code */
105  unsigned short  length;
106  /* maximum length of data packet for your clock format */
107  u_long   flags;
108 /* information for the parser what to look for */
109};
110</pre>
111						<p>The above should have given you some hints on how to build a clk_*.c file with the time code conversion. See the examples and pick a clock closest to yours and tweak the code to match your clock.</p>
112						<p>In order to make your clk_*.c file usable a reference to the clockformat structure must be put into parse_conf.c.</p>
113				</ul>
114			<li>TTY setup and initialisation/configuration will be done in ntpd/refclock_parse.c.
115				<ul>
116					<li>Find out the exact tty settings for your clock (baud rate, parity, stop bits, character size, ...) and note them in terms of termio*.h c_cflag macros.
117					<li>in ntpd/refclock_parse.c fill out a new the struct clockinfo element (that allocates a new &quot;IP&quot; address - see comments) (see all the other clocks for example)
118						<pre>
119   struct clockinfo
120     {
121      u_long  cl_flags;             /* operation flags (io modes) */
122	 PARSE_F_PPSPPS       use loopfilter PPS code (CIOGETEV)
123	 PARSE_F_PPSONSECOND  PPS pulses are on second
124	 usually flags stay 0 as they are used only for special setups
125
126    void  (*cl_poll)();           /* active poll routine */
127         The routine to call when the clock needs data sent to it in order to
128         get a time code from the clock (e.g. Trimble clock)
129
130    int   (*cl_init)();           /* active poll init routine */
131         The routine to call for very special initializations.
132
133    void  (*cl_event)();          /* special event handling (e.g. reset clock) */
134         What to do, when an event happens - used to re-initialize clocks on timeout.
135
136    void  (*cl_end)();            /* active poll end routine */
137         The routine to call to undo any special initialisation (free memory/timers)
138
139    void   *cl_data;              /* local data area for &quot;poll&quot; mechanism */
140         local data for polling routines
141
142    u_fp    cl_rootdelay;         /* rootdelay */
143         NTP rootdelay estimate (usually 0)
144
145	     u_long  cl_basedelay;         /* current offset - unsigned l_fp
146                                              fractional part (fraction) by
147                                              which the RS232 time code is
148                                              delayed from the actual time. */
149
150    u_long  cl_ppsdelay;          /* current PPS offset - unsigned l_fp fractional
151         time (fraction) by which the PPS time stamp is delayed (usually 0)
152   part */
153
154    char   *cl_id;                /* ID code (usually &quot;DCF&quot;) */
155         Refclock id - (max 4 chars)
156
157    char   *cl_description;       /* device name */
158         Name of this device.
159
160    char   *cl_format;            /* fixed format */
161         If the data format cann not ne detected automatically this is the name
162	 as in clk_*.c clockformat.
163
164    u_char  cl_type;              /* clock type (ntp control) */
165         Type if clock as in clock status word (ntp control messages) - usually 0
166
167    u_long  cl_maxunsync;         /* time to trust oscillator after losing synch
168  */
169         seconds a clock can be trusted after losing synchronisation.
170
171    u_long  cl_speed;             /* terminal input &amp; output baudrate */
172    u_long  cl_cflag;             /* terminal io flags */
173    u_long  cl_iflag;             /* terminal io flags */
174    u_long  cl_oflag;             /* terminal io flags */
175    u_long  cl_lflag;             /* terminal io flags */
176         termio*.h tty modes.
177
178    u_long  cl_samples;           /* samples for median filter */
179    u_long  cl_keep;              /* samples for median filter to keep */
180         median filter parameters - smoothing and rejection of bad samples
181  } clockinfo[] = {
182  ...,&lt;other clocks&gt;,...
183  { &lt; your parameters&gt; },
184  };
185
186</pre>
187				</ul>
188		</ol>
189		<p>Well, this is very sketchy, i know. But I hope it helps a little bit. The best way is to look which clock comes closest to your and tweak that code.</p>
190		<p>Two sorts of clocks are used with parse. Clocks that automatically send their time code (once a second) do not need entries in the poll routines because they send the data all the time. The second sort are the clocks that need a command sent to them in order to reply with a time code (like the Trimble clock).</p>
191		<p>For questions: <a href="mailto:%20kardel AT acm.org">kardel@acm.org</a>.</p>
192		<p>Please include an exact description on how your clock works. (initialisation, TTY modes, strings to be sent to it, responses received from the clock).</p>
193		<hr>
194		<script type="text/javascript" language="javascript" src="scripts/footer.txt"></script>
195	</body>
196
197	<body></body>
198
199</html>
200