Commit 871a5acd authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Proper timestamps.

parent 438a6988
......@@ -13,20 +13,20 @@ prrtTimestamp_t PrrtClock_get_current_time_us()
return (prrtTimestamp_t) (1000000 * tv.tv_sec + tv.tv_usec);
}
uint32_t PrrtClock_get_current_time_ms()
prrtTimestamp_t PrrtClock_get_current_time_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint32_t) (1000 * tv.tv_sec + tv.tv_usec / 1000);
}
uint32_t PrrtClock_get_prrt_time_us(PrrtClock *clock)
prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock)
{
uint32_t currentTime = PrrtClock_get_current_time_us();
uint32_t virtualTime = clock->virtualTime;
prrtTimestamp_t currentTime = PrrtClock_get_current_time_us();
prrtTimestamp_t virtualTime = clock->virtualTime;
if(virtualTime) {
uint32_t lastMeasurement = clock->lastMeasurement;
prrtTimestamp_t lastMeasurement = clock->lastMeasurement;
int32_t diff = diff_ts(currentTime, lastMeasurement);
int32_t skew = (diff * clock->skew) / 400;
return virtualTime + clock->meanDeviation + skew;
......@@ -38,10 +38,10 @@ uint32_t PrrtClock_get_prrt_time_us(PrrtClock *clock)
bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
{
uint32_t currentTime = PrrtClock_get_current_time_us();
uint32_t virtualTime = clock->virtualTime;
prrtTimestamp_t currentTime = PrrtClock_get_current_time_us();
prrtTimestamp_t virtualTime = clock->virtualTime;
int32_t clockSkew = clock->skew;
uint32_t delay = rtt >> 1; // half the rtt
prrtTimestamp_t delay = rtt >> 1; // half the rtt
int32_t phaseError = referenceTime - virtualTime + delay;
if(abs(phaseError) > 10000) {
......@@ -56,7 +56,7 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
int32_t period = diff_ts(currentTime, clock->lastMeasurement);
if(period > 0) {
clockSkew = (meanDeviation * 1000 / period) + 15 * clock->skew;
clockSkew = (meanDeviation / period) + 15 * clock->skew;
clockSkew = clockSkew / 16;
}
......
......@@ -6,10 +6,10 @@
#include <stdbool.h>
typedef struct prrtClock {
uint32_t lastMeasurement;
uint32_t meanDeviation;
prrtTimestamp_t lastMeasurement;
prrtTimedelta_t meanDeviation;
int32_t skew;
uint32_t virtualTime;
prrtTimestamp_t virtualTime;
} PrrtClock;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment