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