]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
Fix a bug resetting the samples buffer. Calculate differences each time and store...
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Fri, 19 Mar 2021 08:29:45 +0000 (08:29 +0000)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Fri, 19 Mar 2021 08:29:45 +0000 (08:29 +0000)
nqptp.c

diff --git a/nqptp.c b/nqptp.c
index 8e7db34bff5b8a1f8ecc077c29463d324e2d0853..982860ee2440fee1f29abc3f4236521e396d47c0 100644 (file)
--- a/nqptp.c
+++ b/nqptp.c
@@ -941,6 +941,7 @@ int main(void) {
                             the_clock->vacant_samples =
                                 MAX_TIMING_SAMPLES; // invalidate all the previous samples used for
                                                     // averaging, etc.
+                            the_clock->next_sample_goes_here = 0;
                           }
 
                           // now, store the remote and local times in the array
@@ -948,6 +949,8 @@ int main(void) {
                               the_clock->t2;
                           the_clock->samples[the_clock->next_sample_goes_here].remote =
                               the_clock->t1;
+                          uint64_t diff = the_clock->t1 - the_clock->t2;
+                          the_clock->samples[the_clock->next_sample_goes_here].local_to_remote_offset = diff;
                           the_clock->next_sample_goes_here++;
                           if (the_clock->next_sample_goes_here == MAX_TIMING_SAMPLES)
                             the_clock->next_sample_goes_here = 0;
@@ -1026,12 +1029,11 @@ int main(void) {
                           int e;
                           long double offsets = 0;
                           int sample_count = MAX_TIMING_SAMPLES - the_clock->vacant_samples;
-                          for (e = 0; e < MAX_TIMING_SAMPLES - the_clock->vacant_samples; e++) {
-                            offsets = offsets + 1.0 * (the_clock->samples[e].remote -
-                                                       the_clock->samples[e].local);
+                          for (e = 0; e < sample_count; e++) {
+                            offsets = offsets + 1.0 * (the_clock->samples[e].local_to_remote_offset);
                           }
 
-                          offsets = offsets / (MAX_TIMING_SAMPLES - the_clock->vacant_samples);
+                          offsets = offsets / sample_count;
 
                           // uint64_t offset = (uint64_t)offsets;