#include #include #include /* * Evaluates Generic/DCF, PPS/SHM and average offset * * Copyright (C) 2018 - 2024 R.J. van der Putten, Leiden, Holland, * rob at sput dot nl. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ int main(int argc, char **argv) { FILE *p1; char line[128], remt[32]; int df, i, reach, sumcnt; double avgoff, dcfoff, offset, ppsoff; p1 = NULL; memset(line, 0, 128); memset(remt, 0, 32); df = 0; i = 0; reach = 0; sumcnt = 0; /* Sum count */ avgoff = 0.0; /* Average offset */ dcfoff = 0.0; /* DCF offset */ offset = 0.0; /* Offset currently being read */ ppsoff = 0.0; /* PPS offset */ if (argc > 1) { for (i = 1; i < argc; i++) { if(strcmp(argv[i], "-d") == 0) df = 1; } } if ((p1 = popen("/usr/bin/ntpq -p", "r")) == NULL) { fprintf(stderr, "Cant open: /usr/bin/ntpq -p\n"); exit(1); } avgoff = 0.0; i = 0; sumcnt = 0; while (fgets(line, 128, p1)) { if (i > 1) { /* Ignore first two lines */ if (sscanf(line, "%31s %*s %*s %*s %*s %*s %d %*s %lf %*s", \ remt, &reach, &offset) == 3) { /* [ *+]Remote */ if (strncmp(line + 1, "RAWDCF_CONRAD", 13) == 0 || \ strncmp(line + 1, "127.127.8.", 10) == 0) { dcfoff = offset; } else if (strncmp(line + 1, "SHM", 3) == 0 || \ strncmp(line + 1, "127.127.28.", 11) == 0) { ppsoff = offset; } else if (strncmp(line + 1, "NMEA", 4) != 0 || \ strncmp(line + 1, "127.127.20.", 11) == 0) { //if (reach != 0 && offset > -0.4 && offset < 0.4 ) { if (reach != 0 && offset > -1.0 && offset < 1.0 ) { avgoff = avgoff + offset; if(df != 0) printf("%d %s\t%f\n", sumcnt, remt, offset); sumcnt++; } } } } i++; } pclose(p1); if (sumcnt != 0) avgoff = avgoff / (double) sumcnt; if (df != 0) { printf("Cnt: %d\nDCF: %f\nPPS: %f\nAvg: %f\n", \ sumcnt, dcfoff, ppsoff, avgoff); } if(sumcnt < 2 || \ //ppsoff < -0.2 || ppsoff > 0.2 || ppsoff < -0.5 || ppsoff > 0.5 || \ //avgoff < -0.2 || avgoff > 0.2) avgoff < -0.7 || avgoff > 0.7) /* Dodgy sync */ printf("0\n"); else printf("1\n"); return(0); }