monolithic-serialpps-timepps.h [plain text]
#ifndef _SYS_TIMEPPS_H_
#define _SYS_TIMEPPS_H_
#define PPS_API_VERS_1 1
#define PPS_JAN_1970 2208988800UL
#define PPS_NANOSECOND 1000000000L
#define PPS_FRAC 4294967296.
#define PPS_HECTONANOSECONDS 10000000
#define PPS_FILETIME_1970 0x019db1ded53e8000
#define PPS_NORMALIZE(x) \
do { \
if ((x).tv_nsec >= PPS_NANOSECOND) { \
(x).tv_nsec -= PPS_NANOSECOND; \
(x).tv_sec++; \
} else if ((x).tv_nsec < 0) { \
(x).tv_nsec += PPS_NANOSECOND; \
(x).tv_sec--; \
} \
} while (0)
#define PPS_TSPECTONTP(x) \
do { \
double d_temp; \
\
(x).integral += (unsigned int)PPS_JAN_1970; \
d_temp = (x).fractional * PPS_FRAC / PPS_NANOSECOND; \
if (d_temp >= PPS_FRAC) \
(x).integral++; \
(x).fractional = (unsigned int)d_temp; \
} while (0)
#define PPS_NTPTOTSPEC(x) \
do { \
double d_temp; \
\
(x).tv_sec -= (time_t)PPS_JAN_1970; \
d_temp = (double)((x).tv_nsec); \
d_temp *= PPS_NANOSECOND; \
d_temp /= PPS_FRAC; \
(x).tv_nsec = (long)d_temp; \
} while (0)
#define PPS_CAPTUREASSERT 0x01
#define PPS_CAPTURECLEAR 0x02
#define PPS_CAPTUREBOTH 0x03
#define PPS_OFFSETASSERT 0x10
#define PPS_OFFSETCLEAR 0x20
#define PPS_OFFSETBOTH 0x30
#define PPS_CANWAIT 0x100
#define PPS_CANPOLL 0x200
#define PPS_ECHOASSERT 0x40
#define PPS_ECHOCLEAR 0x80
#define PPS_TSFMT_TSPEC 0x1000
#define PPS_TSFMT_NTPFP 0x2000
#define PPS_KC_HARDPPS 0
#define PPS_KC_HARDPPS_PLL 1
#define PPS_KC_HARDPPS_FLL 2
typedef unsigned long pps_seq_t;
#pragma warning(push)
#pragma warning(disable: 201)
typedef struct ntp_fp {
union ntp_fp_sec {
unsigned int integral;
int s_integral;
};
unsigned int fractional;
} ntp_fp_t;
#pragma warning(pop)
typedef union pps_timeu {
struct timespec tspec;
ntp_fp_t ntpfp;
unsigned long longpad[3];
} pps_timeu_t;
#define NTPFP_M_ADD(r_i, r_f, a_i, a_f) \
do { \
register u_int32 lo_tmp; \
register u_int32 hi_tmp; \
\
lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
if (lo_tmp & 0x10000) \
hi_tmp++; \
(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
\
(r_i) += (a_i); \
if (hi_tmp & 0x10000) \
(r_i)++; \
} while (0)
#define NTPFP_L_ADDS(r, a) NTPFP_M_ADD((r)->integral, (r)->fractional, \
(a)->s_integral, (a)->fractional)
typedef struct pps_info {
pps_seq_t assert_sequence;
pps_seq_t clear_sequence;
pps_timeu_t assert_tu;
pps_timeu_t clear_tu;
int current_mode;
} pps_info_t;
#define assert_timestamp assert_tu.tspec
#define clear_timestamp clear_tu.tspec
#define assert_timestamp_ntpfp assert_tu.ntpfp
#define clear_timestamp_ntpfp clear_tu.ntpfp
typedef struct pps_params {
int api_version;
int mode;
pps_timeu_t assert_off_tu;
pps_timeu_t clear_off_tu;
} pps_params_t;
#define assert_offset assert_off_tu.tspec
#define clear_offset clear_off_tu.tspec
#define assert_offset_ntpfp assert_off_tu.ntpfp
#define clear_offset_ntpfp clear_off_tu.ntpfp
#define PPS_CAP (PPS_CAPTUREASSERT | PPS_OFFSETASSERT | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
#define PPS_RO (PPS_CANWAIT | PPS_CANPOLL)
typedef struct {
int filedes;
OVERLAPPED ol;
pps_params_t params;
} pps_unit_t;
typedef pps_unit_t* pps_handle_t;
#include <windows.h>
#include <errno.h>
#ifndef EOPNOTSUPP
#define EOPNOTSUPP 45
#endif
typedef struct _OLD_SERIAL_PPS_STAMPS {
LARGE_INTEGER Timestamp;
LARGE_INTEGER Counterstamp;
} OLD_SERIAL_PPS_STAMPS, *POLDSERIAL_PPS_STAMPS;
typedef struct _SERIAL_PPS_STAMPS {
LARGE_INTEGER Timestamp;
LARGE_INTEGER Counterstamp;
DWORD SeqNum;
} SERIAL_PPS_STAMPS, *PSERIAL_PPS_STAMPS;
#define IOCTL_SERIAL_GET_PPS_STAMPS CTL_CODE(FILE_DEVICE_SERIAL_PORT,114,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define PPS_OFFSETOF(m,s) (size_t)(&((s *)0)->m)
#define RETURN_PPS_ERRNO(e) \
do { \
SetLastError(NO_ERROR); \
errno = (e); \
return -1; \
} while (0)
#ifdef OWN_PPS_NTP_TIMESTAMP_FROM_COUNTER
extern void pps_ntp_timestamp_from_counter(ntp_fp_t *, ULONGLONG, ULONGLONG);
#else
static inline void
pps_ntp_timestamp_from_counter(
ntp_fp_t *result,
ULONGLONG Timestamp,
ULONGLONG Counterstamp)
{
ULONGLONG BiasedTimestamp;
BiasedTimestamp = Timestamp - PPS_FILETIME_1970;
result->integral = PPS_JAN_1970 +
(unsigned)(BiasedTimestamp / PPS_HECTONANOSECONDS);
result->fractional =
(unsigned) ((BiasedTimestamp % PPS_HECTONANOSECONDS) *
(PPS_FRAC / PPS_HECTONANOSECONDS));
}
#endif
static inline int
time_pps_create(
int filedes,
pps_handle_t *handle
)
{
OLD_SERIAL_PPS_STAMPS old_pps_stamps;
DWORD bytes;
OVERLAPPED ol;
if (!handle)
RETURN_PPS_ERRNO(EFAULT);
if (PPS_OFFSETOF(tspec.tv_nsec, pps_timeu_t) !=
PPS_OFFSETOF(ntpfp.fractional, pps_timeu_t)) {
fprintf(stderr,
"timepps.h needs work, union of \n"
"unsigned int ntp_fp.integral and\n"
"time_t timespec.tv_sec accessed\n"
"interchangeably.\n");
RETURN_PPS_ERRNO(EFAULT);
}
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ol.hEvent = (HANDLE) ((ULONG_PTR)ol.hEvent | 1);
if (FALSE == DeviceIoControl(
(HANDLE)_get_osfhandle(filedes),
IOCTL_SERIAL_GET_PPS_STAMPS,
NULL,
0,
&old_pps_stamps,
sizeof(old_pps_stamps),
&bytes,
&ol)
|| sizeof(old_pps_stamps) != bytes) {
CloseHandle(ol.hEvent);
fprintf(stderr,
"time_pps_create: IOCTL_SERIAL_GET_PPS_STAMPS: %d %d\n",
bytes,
GetLastError());
RETURN_PPS_ERRNO(ENXIO);
}
*handle = malloc(sizeof(pps_unit_t));
if (!(*handle))
RETURN_PPS_ERRNO(ENOMEM);
memset(*handle, 0, sizeof(pps_unit_t));
(*handle)->filedes = filedes;
(*handle)->ol.hEvent = ol.hEvent;
(*handle)->params.api_version = PPS_API_VERS_1;
(*handle)->params.mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
return (0);
}
static inline int
time_pps_destroy(
pps_handle_t handle
)
{
if (!handle)
RETURN_PPS_ERRNO(EBADF);
CloseHandle(handle->ol.hEvent);
free(handle);
return (0);
}
static inline int
time_pps_setparams(
pps_handle_t handle,
const pps_params_t *params
)
{
int mode, mode_in;
if (!handle)
RETURN_PPS_ERRNO(EBADF);
if (!params)
RETURN_PPS_ERRNO(EFAULT);
if (params->api_version != PPS_API_VERS_1)
RETURN_PPS_ERRNO(EINVAL);
mode_in = params->mode;
if ((mode_in & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) ==
(PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) {
if (handle->params.assert_offset.tv_sec ||
handle->params.assert_offset.tv_nsec)
RETURN_PPS_ERRNO(EINVAL);
mode_in &= ~PPS_TSFMT_NTPFP;
}
mode_in &= ~PPS_RO;
if (mode_in & ~(PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP))
RETURN_PPS_ERRNO(EOPNOTSUPP);
mode = handle->params.mode;
handle->params = *params;
handle->params.mode = mode | mode_in;
handle->params.api_version = PPS_API_VERS_1;
return (0);
}
static inline int
time_pps_getparams(
pps_handle_t handle,
pps_params_t *params
)
{
if (!handle)
RETURN_PPS_ERRNO(EBADF);
if (!params)
RETURN_PPS_ERRNO(EFAULT);
*params = handle->params;
return (0);
}
static inline int
time_pps_getcap(
pps_handle_t handle,
int *mode
)
{
if (!handle)
RETURN_PPS_ERRNO(EBADF);
if (!mode)
RETURN_PPS_ERRNO(EFAULT);
*mode = PPS_CAP;
return (0);
}
static inline int
time_pps_fetch(
pps_handle_t handle,
const int tsformat,
pps_info_t *ppsinfo,
const struct timespec *timeout
)
{
SERIAL_PPS_STAMPS pps_stamps;
pps_info_t infobuf;
BOOL rc;
DWORD bytes;
DWORD lasterr;
if (!handle)
RETURN_PPS_ERRNO(EBADF);
if (!ppsinfo)
RETURN_PPS_ERRNO(EFAULT);
memset(&infobuf, 0, sizeof(infobuf));
if (!handle->params.mode & PPS_CAPTUREASSERT) {
*ppsinfo = infobuf;
return (0);
}
pps_stamps.SeqNum = 0;
bytes = 0;
rc = DeviceIoControl(
(HANDLE)_get_osfhandle(handle->filedes),
IOCTL_SERIAL_GET_PPS_STAMPS,
NULL,
0,
&pps_stamps,
sizeof(pps_stamps),
&bytes,
&handle->ol);
if (!rc) {
lasterr = GetLastError();
if (ERROR_INVALID_PARAMETER != lasterr)
fprintf(stderr, "time_pps_fetch: ioctl err %d\n",
lasterr);
RETURN_PPS_ERRNO(EOPNOTSUPP);
} else if (bytes != sizeof(pps_stamps) &&
bytes != sizeof(OLD_SERIAL_PPS_STAMPS)) {
fprintf(stderr,
"time_pps_fetch: wanted %d or %d bytes got %d from "
"IOCTL_SERIAL_GET_PPS_STAMPS 0x%x\n" ,
sizeof(OLD_SERIAL_PPS_STAMPS),
sizeof(SERIAL_PPS_STAMPS),
bytes,
IOCTL_SERIAL_GET_PPS_STAMPS);
RETURN_PPS_ERRNO(ENXIO);
}
pps_ntp_timestamp_from_counter(
&infobuf.assert_timestamp_ntpfp,
pps_stamps.Timestamp.QuadPart,
pps_stamps.Counterstamp.QuadPart);
infobuf.assert_sequence = pps_stamps.SeqNum;
switch (tsformat) {
case PPS_TSFMT_NTPFP:
if (handle->params.mode & PPS_OFFSETASSERT) {
NTPFP_L_ADDS(&infobuf.assert_timestamp_ntpfp,
&handle->params.assert_offset_ntpfp);
}
break;
case PPS_TSFMT_TSPEC:
PPS_NTPTOTSPEC(infobuf.assert_timestamp);
if (handle->params.mode & PPS_OFFSETASSERT) {
infobuf.assert_timestamp.tv_sec +=
handle->params.assert_offset.tv_sec;
infobuf.assert_timestamp.tv_nsec +=
handle->params.assert_offset.tv_nsec;
PPS_NORMALIZE(infobuf.assert_timestamp);
}
break;
default:
RETURN_PPS_ERRNO(EINVAL);
}
infobuf.current_mode = handle->params.mode;
*ppsinfo = infobuf;
return (0);
}
static inline int
time_pps_kcbind(
pps_handle_t handle,
const int kernel_consumer,
const int edge, const int tsformat
)
{
if (!handle)
RETURN_PPS_ERRNO(EBADF);
RETURN_PPS_ERRNO(EOPNOTSUPP);
}
#endif