Implement timeouts with log seconds less than zero.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2012-01-08 10:52:39 +01:00
parent c0ebf12f79
commit 6d73cea86a
1 changed files with 34 additions and 26 deletions

60
port.c
View File

@ -31,6 +31,7 @@
#include "port.h" #include "port.h"
#include "print.h" #include "print.h"
#include "tmtab.h" #include "tmtab.h"
#include "tmv.h"
#include "util.h" #include "util.h"
#define PTP_VERSION 2 #define PTP_VERSION 2
@ -121,6 +122,33 @@ static int pid_eq(struct PortIdentity *a, struct PortIdentity *b)
return 0 == memcmp(a, b, sizeof(*a)); return 0 == memcmp(a, b, sizeof(*a));
} }
static int set_tmo(int fd, unsigned int scale, int log_seconds)
{
struct itimerspec tmo = {
{0, 0}, {0, 0}
};
uint64_t ns;
int i;
if (log_seconds < 0) {
log_seconds *= -1;
for (i = 1, ns = scale * 500000000ULL; i < log_seconds; i++) {
ns >>= 1;
}
tmo.it_value.tv_nsec = ns;
while (tmo.it_value.tv_nsec >= NS_PER_SEC) {
tmo.it_value.tv_nsec -= NS_PER_SEC;
tmo.it_value.tv_sec++;
}
} else
tmo.it_value.tv_sec = scale * (1 << log_seconds);
return timerfd_settime(fd, 0, &tmo, NULL);
}
static void fc_clear(struct foreign_clock *fc) static void fc_clear(struct foreign_clock *fc)
{ {
struct ptp_message *m; struct ptp_message *m;
@ -249,14 +277,8 @@ static int port_ignore(struct port *p, struct ptp_message *m)
static int port_set_announce_tmo(struct port *p) static int port_set_announce_tmo(struct port *p)
{ {
struct itimerspec tmo = { return set_tmo(p->fda.fd[FD_ANNOUNCE_TIMER],
{0, 0}, {0, 0} p->announceReceiptTimeout, p->logAnnounceInterval);
};
tmo.it_value.tv_sec =
p->announceReceiptTimeout * (1 << p->logAnnounceInterval);
return timerfd_settime(p->fda.fd[FD_ANNOUNCE_TIMER], 0, &tmo, NULL);
} }
static int port_set_delay_tmo(struct port *p) static int port_set_delay_tmo(struct port *p)
@ -271,32 +293,18 @@ static int port_set_delay_tmo(struct port *p)
static int port_set_manno_tmo(struct port *p) static int port_set_manno_tmo(struct port *p)
{ {
struct itimerspec tmo = { return set_tmo(p->fda.fd[FD_MANNO_TIMER], 1, p->logAnnounceInterval);
{0, 0}, {0, 0}
};
tmo.it_value.tv_sec = (1 << p->logAnnounceInterval);
return timerfd_settime(p->fda.fd[FD_MANNO_TIMER], 0, &tmo, NULL);
} }
static int port_set_qualification_tmo(struct port *p) static int port_set_qualification_tmo(struct port *p)
{ {
struct itimerspec tmo = { return set_tmo(p->fda.fd[FD_QUALIFICATION_TIMER],
{0, 0}, {0, 0} 1+clock_steps_removed(p->clock), p->logAnnounceInterval);
};
tmo.it_value.tv_sec = (1 + clock_steps_removed(p->clock)) *
(1 << p->logAnnounceInterval);
return timerfd_settime(p->fda.fd[FD_QUALIFICATION_TIMER], 0, &tmo, NULL);
} }
static int port_set_sync_tmo(struct port *p) static int port_set_sync_tmo(struct port *p)
{ {
struct itimerspec tmo = { return set_tmo(p->fda.fd[FD_SYNC_TIMER], 1, p->logSyncInterval);
{0, 0}, {0, 0}
};
tmo.it_value.tv_sec = (1 << p->logSyncInterval);
return timerfd_settime(p->fda.fd[FD_SYNC_TIMER], 0, &tmo, NULL);
} }
static void port_synchronize(struct port *p, static void port_synchronize(struct port *p,