#ifndef _NTP_H_
#define _NTP_H_
struct ntp_timestamp64 {
uint32_t seconds;
uint32_t fraction;
};
struct ntp_timestamp32 {
uint16_t seconds;
uint16_t fraction;
};
static inline int ntp_timestamp64_diff_us(const struct ntp_timestamp64 *t1,
const struct ntp_timestamp64 *t2,
int64_t *d)
{
if (t1 == NULL || t2 == NULL || d == NULL)
return -EINVAL;
*d = ((int64_t)t1->seconds - (int64_t)t2->seconds) * 1000000;
*d += (((int64_t)t1->fraction - (int64_t)t2->fraction) * 1000000) >> 32;
return 0;
}
static inline int ntp_timestamp64_to_us(const struct ntp_timestamp64 *t,
uint64_t *us)
{
if (t == NULL || us == NULL)
return -EINVAL;
*us = ((uint64_t)t->seconds * 1000000) +
(((uint64_t)t->fraction * 1000000) >> 32);
return 0;
}
static inline int ntp_timestamp64_from_us(struct ntp_timestamp64 *t,
uint64_t us)
{
if (t == NULL)
return -EINVAL;
t->seconds = us / 1000000;
t->fraction = ((uint64_t)(us % 1000000) << 32) / 1000000;
return 0;
}
static inline int ntp_timestamp64_to_timespec(const struct ntp_timestamp64 *t,
struct timespec *ts)
{
if (t == NULL || ts == NULL)
return -EINVAL;
ts->tv_sec = t->seconds;
ts->tv_nsec = ((uint64_t)t->fraction * 1000000000) >> 32;
return 0;
}
static inline int ntp_timestamp64_from_timespec(struct ntp_timestamp64 *t,
const struct timespec *ts)
{
if (t == NULL || ts == NULL)
return -EINVAL;
t->seconds = ts->tv_sec;
t->fraction = ((uint64_t)ts->tv_nsec << 32) / 1000000000;
return 0;
}
static inline int
ntp_timestamp64_to_ntp_timestamp32(const struct ntp_timestamp64 *t1,
struct ntp_timestamp32 *t2)
{
if (t1 == NULL || t2 == NULL)
return -EINVAL;
t2->seconds = t1->seconds & 0xffff;
t2->fraction = (t1->fraction >> 16) & 0xffff;
return 0;
}
static inline int ntp_timestamp32_diff_us(const struct ntp_timestamp32 *t1,
const struct ntp_timestamp32 *t2,
int64_t *d)
{
if (t1 == NULL || t2 == NULL || d == NULL)
return -EINVAL;
*d = ((int64_t)t1->seconds - (int64_t)t2->seconds) * 1000000;
*d += (((int64_t)t1->fraction - (int64_t)t2->fraction) * 1000000) >> 16;
return 0;
}
static inline int ntp_timestamp32_to_us(const struct ntp_timestamp32 *t,
uint64_t *us)
{
if (t == NULL || us == NULL)
return -EINVAL;
*us = ((uint64_t)t->seconds * 1000000) +
(((uint64_t)t->fraction * 1000000) >> 16);
return 0;
}
static inline int ntp_timestamp32_from_us(struct ntp_timestamp32 *t,
uint64_t us)
{
if (t == NULL)
return -EINVAL;
t->seconds = us / 1000000;
t->fraction = ((uint64_t)(us % 1000000) << 16) / 1000000;
return 0;
}
static inline int ntp_timestamp32_to_timespec(const struct ntp_timestamp32 *t,
struct timespec *ts)
{
if (t == NULL || ts == NULL)
return -EINVAL;
ts->tv_sec = t->seconds;
ts->tv_nsec = ((uint64_t)t->fraction * 1000000000) >> 16;
return 0;
}
static inline int ntp_timestamp32_from_timespec(struct ntp_timestamp32 *t,
const struct timespec *ts)
{
if (t == NULL || ts == NULL)
return -EINVAL;
t->seconds = ts->tv_sec;
t->fraction = ((uint64_t)ts->tv_nsec << 16) / 1000000000;
return 0;
}
static inline int
ntp_timestamp32_to_ntp_timestamp64(const struct ntp_timestamp32 *t1,
struct ntp_timestamp64 *t2)
{
if (t1 == NULL || t2 == NULL)
return -EINVAL;
t2->seconds = t1->seconds;
t2->fraction = t1->fraction << 16;
return 0;
}
#endif