[Linuxptp-users] How pch2sys calcs the offset
PTP IEEE 1588 stack for Linux
Brought to you by:
rcochran
From: Ledda W. E. <Wil...@it...> - 2013-07-25 15:01:34
|
Hi all, I'm not sure if this is the right place to ask this question or if I have to use the devel mail list, but I have a question about phc2sys work. I was interested in how it calcs the offset between the system clock (destination) and the source clock (phc). Exploring the source code I found the read_phc function. struct timespec tdst1, tdst2, tsrc; int i; int64_t interval, best_interval = INT64_MAX; /* Pick the quickest clkid reading. */ for (i = 0; i < readings; i++) { if (clock_gettime(sysclk, &tdst1) || clock_gettime(clkid, &tsrc) || clock_gettime(sysclk, &tdst2)) { perror("clock_gettime"); return 0; } interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC + tdst2.tv_nsec - tdst1.tv_nsec; if (best_interval > interval) { best_interval = interval; *offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC + tdst1.tv_nsec - tsrc.tv_nsec + interval / 2; *ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec; } } return 1; It's quite clear how it works but really I don't understand very well the following operation: *offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC + tdst1.tv_nsec - tsrc.tv_nsec + interval / 2; Can someone explain to me why the half interval is added at the end? Thanks William |