1 /* $NetBSD: ntp_refclock.h,v 1.1.1.3 2013/12/27 23:30:45 christos Exp $ */ 2 3 /* 4 * ntp_refclock.h - definitions for reference clock support 5 */ 6 7 #ifndef NTP_REFCLOCK_H 8 #define NTP_REFCLOCK_H 9 10 #if defined(HAVE_SYS_MODEM_H) 11 #include <sys/modem.h> 12 #endif 13 14 #include "ntp_types.h" 15 #include "ntp_tty.h" 16 #include "recvbuff.h" 17 18 19 #define SAMPLE(x) pp->coderecv = (pp->coderecv + 1) % MAXSTAGE; \ 20 pp->filter[pp->coderecv] = (x); \ 21 if (pp->coderecv == pp->codeproc) \ 22 pp->codeproc = (pp->codeproc + 1) % MAXSTAGE; 23 24 /* 25 * Macros to determine the clock type and unit numbers from a 26 * 127.127.t.u address 27 */ 28 #define REFCLOCKTYPE(srcadr) ((SRCADR(srcadr) >> 8) & 0xff) 29 #define REFCLOCKUNIT(srcadr) (SRCADR(srcadr) & 0xff) 30 31 /* 32 * List of reference clock names and descriptions. These must agree with 33 * lib/clocktypes.c and ntpd/refclock_conf.c. 34 */ 35 struct clktype { 36 int code; /* driver "major" number */ 37 const char *clocktype; /* long description */ 38 const char *abbrev; /* short description */ 39 }; 40 extern struct clktype clktypes[]; 41 42 /* 43 * Configuration flag values 44 */ 45 #define CLK_HAVETIME1 0x1 46 #define CLK_HAVETIME2 0x2 47 #define CLK_HAVEVAL1 0x4 48 #define CLK_HAVEVAL2 0x8 49 50 #define CLK_FLAG1 0x1 51 #define CLK_FLAG2 0x2 52 #define CLK_FLAG3 0x4 53 #define CLK_FLAG4 0x8 54 55 #define CLK_HAVEFLAG1 0x10 56 #define CLK_HAVEFLAG2 0x20 57 #define CLK_HAVEFLAG3 0x40 58 #define CLK_HAVEFLAG4 0x80 59 60 /* 61 * Constant for disabling event reporting in 62 * refclock_receive. ORed in leap 63 * parameter 64 */ 65 #define REFCLOCK_OWN_STATES 0x80 66 67 /* 68 * Structure for returning clock status 69 */ 70 struct refclockstat { 71 u_char type; /* clock type */ 72 u_char flags; /* clock flags */ 73 u_char haveflags; /* bit array of valid flags */ 74 u_short lencode; /* length of last timecode */ 75 const char *p_lastcode; /* last timecode received */ 76 u_int32 polls; /* transmit polls */ 77 u_int32 noresponse; /* no response to poll */ 78 u_int32 badformat; /* bad format timecode received */ 79 u_int32 baddata; /* invalid data timecode received */ 80 u_int32 timereset; /* driver resets */ 81 const char *clockdesc; /* ASCII description */ 82 double fudgetime1; /* configure fudge time1 */ 83 double fudgetime2; /* configure fudge time2 */ 84 int32 fudgeval1; /* configure fudge value1 */ 85 u_int32 fudgeval2; /* configure fudge value2 */ 86 u_char currentstatus; /* clock status */ 87 u_char lastevent; /* last exception event */ 88 u_char leap; /* leap bits */ 89 struct ctl_var *kv_list; /* additional variables */ 90 }; 91 92 /* 93 * Reference clock I/O structure. Used to provide an interface between 94 * the reference clock drivers and the I/O module. 95 */ 96 struct refclockio { 97 struct refclockio *next; /* link to next structure */ 98 void (*clock_recv) (struct recvbuf *); /* completion routine */ 99 int (*io_input) (struct recvbuf *); /* input routine - 100 to avoid excessive buffer use 101 due to small bursts 102 of refclock input data */ 103 struct peer *srcclock; /* refclock peer */ 104 int datalen; /* length of data */ 105 int fd; /* file descriptor */ 106 u_long recvcount; /* count of receive completions */ 107 int active; /* nonzero when in use */ 108 109 #ifdef HAVE_IO_COMPLETION_PORT 110 void * device_context; /* device-related data for i/o subsystem */ 111 #endif 112 }; 113 114 /* 115 * Structure for returning debugging info 116 */ 117 #define NCLKBUGVALUES 16 118 #define NCLKBUGTIMES 32 119 120 struct refclockbug { 121 u_char nvalues; /* values following */ 122 u_char ntimes; /* times following */ 123 u_short svalues; /* values format sign array */ 124 u_int32 stimes; /* times format sign array */ 125 u_int32 values[NCLKBUGVALUES]; /* real values */ 126 l_fp times[NCLKBUGTIMES]; /* real times */ 127 }; 128 129 #ifdef HAVE_IO_COMPLETION_PORT 130 extern HANDLE WaitableIoEventHandle; 131 #endif 132 133 /* 134 * Structure interface between the reference clock support 135 * ntp_refclock.c and the driver utility routines 136 */ 137 #define MAXSTAGE 60 /* max median filter stages */ 138 #define NSTAGE 5 /* default median filter stages */ 139 #define BMAX 128 /* max timecode length */ 140 #define GMT 0 /* I hope nobody sees this */ 141 #define MAXDIAL 60 /* max length of modem dial strings */ 142 143 144 struct refclockproc { 145 void * unitptr; /* pointer to unit structure */ 146 struct refclock * conf; /* refclock_conf[type] */ 147 struct refclockio io; /* I/O handler structure */ 148 u_char leap; /* leap/synchronization code */ 149 u_char currentstatus; /* clock status */ 150 u_char lastevent; /* last exception event */ 151 u_char type; /* clock type */ 152 const char *clockdesc; /* clock description */ 153 u_long nextaction; /* local activity timeout */ 154 void (*action)(struct peer *); /* timeout callback */ 155 156 char a_lastcode[BMAX]; /* last timecode received */ 157 int lencode; /* length of last timecode */ 158 159 int year; /* year of eternity */ 160 int day; /* day of year */ 161 int hour; /* hour of day */ 162 int minute; /* minute of hour */ 163 int second; /* second of minute */ 164 long nsec; /* nanosecond of second */ 165 u_long yearstart; /* beginning of year */ 166 int coderecv; /* put pointer */ 167 int codeproc; /* get pointer */ 168 l_fp lastref; /* reference timestamp */ 169 l_fp lastrec; /* receive timestamp */ 170 double offset; /* mean offset */ 171 double disp; /* sample dispersion */ 172 double jitter; /* jitter (mean squares) */ 173 double filter[MAXSTAGE]; /* median filter */ 174 175 /* 176 * Configuration data 177 */ 178 double fudgetime1; /* fudge time1 */ 179 double fudgetime2; /* fudge time2 */ 180 u_char stratum; /* server stratum */ 181 u_int32 refid; /* reference identifier */ 182 u_char sloppyclockflag; /* fudge flags */ 183 184 /* 185 * Status tallies 186 */ 187 u_long timestarted; /* time we started this */ 188 u_long polls; /* polls sent */ 189 u_long noreply; /* no replies to polls */ 190 u_long badformat; /* bad format reply */ 191 u_long baddata; /* bad data reply */ 192 }; 193 194 /* 195 * Structure interface between the reference clock support 196 * ntp_refclock.c and particular clock drivers. This must agree with the 197 * structure defined in the driver. 198 */ 199 #define noentry 0 /* flag for null routine */ 200 #define NOFLAGS 0 /* flag for null flags */ 201 202 struct refclock { 203 int (*clock_start) (int, struct peer *); 204 void (*clock_shutdown) (int, struct peer *); 205 void (*clock_poll) (int, struct peer *); 206 void (*clock_control) (int, const struct refclockstat *, 207 struct refclockstat *, struct peer *); 208 void (*clock_init) (void); 209 void (*clock_buginfo) (int, struct refclockbug *, struct peer *); 210 void (*clock_timer) (int, struct peer *); 211 }; 212 213 /* 214 * Function prototypes 215 */ 216 extern int io_addclock (struct refclockio *); 217 extern void io_closeclock (struct refclockio *); 218 219 #ifdef REFCLOCK 220 extern void refclock_buginfo(sockaddr_u *, 221 struct refclockbug *); 222 extern void refclock_control(sockaddr_u *, 223 const struct refclockstat *, 224 struct refclockstat *); 225 extern int refclock_open (char *, u_int, u_int); 226 extern int refclock_setup (int, u_int, u_int); 227 extern void refclock_timer (struct peer *); 228 extern void refclock_transmit(struct peer *); 229 extern int refclock_process(struct refclockproc *); 230 extern int refclock_process_f(struct refclockproc *, double); 231 extern void refclock_process_offset(struct refclockproc *, l_fp, 232 l_fp, double); 233 extern void refclock_report (struct peer *, int); 234 extern int refclock_gtlin (struct recvbuf *, char *, int, l_fp *); 235 extern int refclock_gtraw (struct recvbuf *, char *, int, l_fp *); 236 extern int indicate_refclock_packet(struct refclockio *, 237 struct recvbuf *); 238 extern void process_refclock_packet(struct recvbuf *); 239 #endif /* REFCLOCK */ 240 241 #endif /* NTP_REFCLOCK_H */ 242