/contrib/ntp/util/pps-api.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 100 lines · 77 code · 14 blank · 9 comment · 12 complexity · 7af591a0c1c1ee8e25bd759c2a704cea MD5 · raw file

  1. /*
  2. Try to run this program to see what the PPS-API finds. You give it the
  3. device as argument and you may have to modify the pp.mode = BLA assignment.
  4. Poul-Henning
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <err.h>
  10. #include <sys/types.h>
  11. #include <time.h>
  12. #include <sys/timepps.h>
  13. #include <sys/termios.h>
  14. #define timespecsub(vvp, uvp) \
  15. do { \
  16. (vvp)->tv_sec -= (uvp)->tv_sec; \
  17. (vvp)->tv_nsec -= (uvp)->tv_nsec; \
  18. if ((vvp)->tv_nsec < 0) { \
  19. (vvp)->tv_sec--; \
  20. (vvp)->tv_nsec += 1000000000; \
  21. } \
  22. } while (0)
  23. void
  24. Chew(struct timespec *tsa, struct timespec *tsc, unsigned sa, unsigned sc)
  25. {
  26. static int idx;
  27. struct timespec ts;
  28. printf("%d.%09d ", tsa->tv_sec, tsa->tv_nsec);
  29. printf("%d.%09d ", tsc->tv_sec, tsc->tv_nsec);
  30. printf("%u %u ", sa, sc);
  31. ts = *tsc;
  32. timespecsub(&ts,tsa);
  33. printf("%.9f ", ts.tv_sec + ts.tv_nsec / 1e9);
  34. printf("\n");
  35. fflush(stdout);
  36. }
  37. int
  38. main(int argc, char **argv)
  39. {
  40. int fd;
  41. pps_info_t pi;
  42. pps_params_t pp;
  43. pps_handle_t ph;
  44. int i, mode;
  45. u_int olda, oldc;
  46. double d = 0;
  47. struct timespec to;
  48. if (argc < 2)
  49. argv[1] = "/dev/cuaa1";
  50. setbuf(stdout, 0);
  51. fd = open(argv[1], O_RDONLY);
  52. if (fd < 0)
  53. err(1, argv[1]);
  54. i = time_pps_create(fd, &ph);
  55. if (i < 0)
  56. err(1, "time_pps_create");
  57. i = time_pps_getcap(ph, &mode);
  58. if (i < 0)
  59. err(1, "time_pps_getcap");
  60. pp.mode = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
  61. pp.mode = PPS_CAPTUREBOTH;
  62. /* pp.mode = PPS_CAPTUREASSERT; */
  63. i = time_pps_setparams(ph, &pp);
  64. if (i < 0)
  65. err(1, "time_pps_setparams");
  66. while (1) {
  67. to.tv_nsec = 0;
  68. to.tv_sec = 0;
  69. i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
  70. if (i < 0)
  71. err(1, "time_pps_fetch");
  72. if (olda == pi.assert_sequence &&
  73. oldc == pi.clear_sequence) {
  74. usleep(10000);
  75. continue;
  76. }
  77. Chew(&pi.assert_timestamp, &pi.clear_timestamp,
  78. pi.assert_sequence, pi.clear_sequence);
  79. olda = pi.assert_sequence;
  80. oldc = pi.clear_sequence;
  81. }
  82. return(0);
  83. }