/contrib/ntp/libparse/clk_schmid.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 258 lines · 134 code · 28 blank · 96 comment · 25 complexity · 9dfb324b9054a1fa1eb032643b3398bb MD5 · raw file

  1. /*
  2. * /src/NTP/ntp4-dev/libparse/clk_schmid.c,v 4.9 2005/04/16 17:32:10 kardel RELEASE_20050508_A
  3. *
  4. * clk_schmid.c,v 4.9 2005/04/16 17:32:10 kardel RELEASE_20050508_A
  5. *
  6. * Schmid clock support
  7. * based on information and testing from Adam W. Feigin et. al (Swisstime iis.ethz.ch)
  8. *
  9. * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
  10. * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the author nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. */
  37. #if HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_SCHMID)
  41. #include "ntp_fp.h"
  42. #include "ntp_unixtime.h"
  43. #include "ntp_calendar.h"
  44. #include "parse.h"
  45. #ifndef PARSESTREAM
  46. #include "ntp_stdlib.h"
  47. #include <stdio.h>
  48. #else
  49. #include "sys/parsestreams.h"
  50. extern void printf P((const char *, ...));
  51. #endif
  52. /*
  53. * Description courtesy of Adam W. Feigin et. al (Swisstime iis.ethz.ch)
  54. *
  55. * The command to Schmid's DCF77 clock is a single byte; each bit
  56. * allows the user to select some part of the time string, as follows (the
  57. * output for the lsb is sent first).
  58. *
  59. * Bit 0: time in MEZ, 4 bytes *binary, not BCD*; hh.mm.ss.tenths
  60. * Bit 1: date 3 bytes *binary, not BCD: dd.mm.yy
  61. * Bit 2: week day, 1 byte (unused here)
  62. * Bit 3: time zone, 1 byte, 0=MET, 1=MEST. (unused here)
  63. * Bit 4: clock status, 1 byte, 0=time invalid,
  64. * 1=time from crystal backup,
  65. * 3=time from DCF77
  66. * Bit 5: transmitter status, 1 byte,
  67. * bit 0: backup antenna
  68. * bit 1: time zone change within 1h
  69. * bit 3,2: TZ 01=MEST, 10=MET
  70. * bit 4: leap second will be
  71. * added within one hour
  72. * bits 5-7: Zero
  73. * Bit 6: time in backup mode, units of 5 minutes (unused here)
  74. *
  75. */
  76. #define WS_TIME 0x01
  77. #define WS_SIGNAL 0x02
  78. #define WS_ALTERNATE 0x01
  79. #define WS_ANNOUNCE 0x02
  80. #define WS_TZ 0x0c
  81. #define WS_MET 0x08
  82. #define WS_MEST 0x04
  83. #define WS_LEAP 0x10
  84. static u_long cvt_schmid P((unsigned char *, int, struct format *, clocktime_t *, void *));
  85. static unsigned long inp_schmid P((parse_t *, unsigned int, timestamp_t *));
  86. clockformat_t clock_schmid =
  87. {
  88. inp_schmid, /* no input handling */
  89. cvt_schmid, /* Schmid conversion */
  90. 0, /* not direct PPS monitoring */
  91. 0, /* conversion configuration */
  92. "Schmid", /* Schmid receiver */
  93. 12, /* binary data buffer */
  94. 0, /* no private data (complete messages) */
  95. };
  96. static u_long
  97. cvt_schmid(
  98. unsigned char *buffer,
  99. int size,
  100. struct format *format,
  101. clocktime_t *clock_time,
  102. void *local
  103. )
  104. {
  105. if ((size != 11) || (buffer[10] != (unsigned char)'\375'))
  106. {
  107. return CVT_NONE;
  108. }
  109. else
  110. {
  111. if (buffer[0] > 23 || buffer[1] > 59 || buffer[2] > 59 || buffer[3] > 9) /* Time */
  112. {
  113. return CVT_FAIL|CVT_BADTIME;
  114. }
  115. else
  116. if (buffer[4] < 1 || buffer[4] > 31 || buffer[5] < 1 || buffer[5] > 12
  117. || buffer[6] > 99)
  118. {
  119. return CVT_FAIL|CVT_BADDATE;
  120. }
  121. else
  122. {
  123. clock_time->hour = buffer[0];
  124. clock_time->minute = buffer[1];
  125. clock_time->second = buffer[2];
  126. clock_time->usecond = buffer[3] * 100000;
  127. clock_time->day = buffer[4];
  128. clock_time->month = buffer[5];
  129. clock_time->year = buffer[6];
  130. clock_time->flags = 0;
  131. switch (buffer[8] & WS_TZ)
  132. {
  133. case WS_MET:
  134. clock_time->utcoffset = -1*60*60;
  135. break;
  136. case WS_MEST:
  137. clock_time->utcoffset = -2*60*60;
  138. clock_time->flags |= PARSEB_DST;
  139. break;
  140. default:
  141. return CVT_FAIL|CVT_BADFMT;
  142. }
  143. if (!(buffer[7] & WS_TIME))
  144. {
  145. clock_time->flags |= PARSEB_POWERUP;
  146. }
  147. if (!(buffer[7] & WS_SIGNAL))
  148. {
  149. clock_time->flags |= PARSEB_NOSYNC;
  150. }
  151. if (buffer[7] & WS_SIGNAL)
  152. {
  153. if (buffer[8] & WS_ALTERNATE)
  154. {
  155. clock_time->flags |= PARSEB_ALTERNATE;
  156. }
  157. if (buffer[8] & WS_ANNOUNCE)
  158. {
  159. clock_time->flags |= PARSEB_ANNOUNCE;
  160. }
  161. if (buffer[8] & WS_LEAP)
  162. {
  163. clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */
  164. }
  165. }
  166. clock_time->flags |= PARSEB_S_LEAP|PARSEB_S_ANTENNA;
  167. return CVT_OK;
  168. }
  169. }
  170. }
  171. /*
  172. * inp_schmid
  173. *
  174. * grep data from input stream
  175. */
  176. static u_long
  177. inp_schmid(
  178. parse_t *parseio,
  179. unsigned int ch,
  180. timestamp_t *tstamp
  181. )
  182. {
  183. unsigned int rtc;
  184. parseprintf(DD_PARSE, ("inp_schmid(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
  185. switch (ch)
  186. {
  187. case 0xFD: /* */
  188. parseprintf(DD_PARSE, ("mbg_input: ETX seen\n"));
  189. if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
  190. return parse_end(parseio);
  191. else
  192. return rtc;
  193. default:
  194. return parse_addchar(parseio, ch);
  195. }
  196. }
  197. #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SCHMID) */
  198. int clk_schmid_bs;
  199. #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SCHMID) */
  200. /*
  201. * History:
  202. *
  203. * clk_schmid.c,v
  204. * Revision 4.9 2005/04/16 17:32:10 kardel
  205. * update copyright
  206. *
  207. * Revision 4.8 2004/11/14 15:29:41 kardel
  208. * support PPSAPI, upgrade Copyright to Berkeley style
  209. *
  210. * Revision 4.5 1999/11/28 09:13:51 kardel
  211. * RECON_4_0_98F
  212. *
  213. * Revision 4.4 1998/06/13 12:06:03 kardel
  214. * fix SYSV clock name clash
  215. *
  216. * Revision 4.3 1998/06/12 15:22:29 kardel
  217. * fix prototypes
  218. *
  219. * Revision 4.2 1998/06/12 09:13:26 kardel
  220. * conditional compile macros fixed
  221. * printf prototype
  222. *
  223. * Revision 4.1 1998/05/24 09:39:53 kardel
  224. * implementation of the new IO handling model
  225. *
  226. * Revision 4.0 1998/04/10 19:45:31 kardel
  227. * Start 4.0 release version numbering
  228. *
  229. * from V3 3.22 log info deleted 1998/04/11 kardel
  230. */