clock.h 1.07 KB
Newer Older
1 2 3
#ifndef PRRT_CLOCK_H
#define PRRT_CLOCK_H

4
#include "types/packet.h"
5
#include <stdint.h>
6
#include <stdbool.h>
7

8 9 10
#define MAX_TIMESTAMP (prrtTimestamp_t) 4294967295
#define HALF_TIMESTAMP (prrtTimestamp_t) 2147483648

11 12 13
#define usleep_nano(t) nanosleep((const struct timespec[]){{0, t * 1000}}, NULL)
#define sleep_nano(t) nanosleep((const struct timespec[]){{t, 0}}, NULL)

Andreas Schmidt's avatar
Andreas Schmidt committed
14

15
typedef struct prrtClock {
Andreas Schmidt's avatar
Andreas Schmidt committed
16
    prrtTimestamp_t lastMeasurement;
Andreas Schmidt's avatar
Andreas Schmidt committed
17 18
    prrtTimeDifference_t meanDeviation;
    prrtTimeDifference_t skew;
Andreas Schmidt's avatar
Andreas Schmidt committed
19
    prrtTimestamp_t virtualTime;
20 21 22
} PrrtClock;


Stefan Reif's avatar
Stefan Reif committed
23
void PrrtClock_init(PrrtClock *clock);
24

Andreas Schmidt's avatar
Andreas Schmidt committed
25
prrtTimestamp_t PrrtClock_get_current_time_us(void);
26

27
prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock);
28

Andreas Schmidt's avatar
Andreas Schmidt committed
29
bool PrrtClock_update(PrrtClock *clock, prrtTimestamp_t referenceTime, prrtTimedelta_t propagation_delay);
30

Andreas Schmidt's avatar
Andreas Schmidt committed
31 32
#define diff_abs_ts(timeA, timeB) (prrtTimeDifference_t) abs(((prrtTimeDifference_t) timeA) - ((prrtTimeDifference_t)timeB))

33
#define PrrtClock_TimespecToPrrtTimestamp(timespec) ((prrtTimestamp_t) (1000000 * timespec.tv_sec + timespec.tv_nsec / 1000))
34

35
#endif //PRRT_CLOCK_H