| PPS-Client
    2.0.0
    Client for synchronizing the system clock to a GPS PPS source | 
 
 
 
Go to the documentation of this file.
   37 #include <sys/timex.h> 
   40 #include <sys/types.h> 
   47 #define USECS_PER_SEC 1000000 
   48 #define SECS_PER_MINUTE 60 
   49 #define SECS_PER_5_MIN 300 
   50 #define SECS_PER_10_MIN 600 
   51 #define SECS_PER_HOUR 3600 
   52 #define SECS_PER_DAY 86400 
   53 #define NUM_5_MIN_INTERVALS 288              
   54 #define FIVE_MINUTES 5 
   55 #define PER_MINUTE (1.0 / (double)SECS_PER_MINUTE) 
   56 #define SETTLE_TIME (2 * SECS_PER_10_MIN)    
   59 #define INTEGRAL_GAIN 0.63212                
   60 #define FREQDIFF_INTRVL 5                    
   61 #define PPS_WINDOW 500                       
   62 #define PTHREAD_STACK_REQUIRED 16384         
   64 #define ZERO_OFFSET_RPI3 8 
   65 #define ZERO_OFFSET_RPI4 4 
   67 #define OFFSETFIFO_LEN 80                    
   68 #define NUM_INTEGRALS 10                     
   69 #define PER_NUM_INTEGRALS (1.0 / (double)NUM_INTEGRALS)  
   71 #define ADJTIMEX_SCALE 65536.0               
   73 #define RAW_ERROR_ZERO  20                   
   74 #define RAW_ERROR_DECAY 0.98851              
   76 #define INTERRUPT_LOST 15                    
   79 #define CHECK_TIME 1024                      
   80 #define BLOCK_FOR_10 10                      
   82 #define CHECK_TIME_SERIAL 10                 
   85 #define MAX_SPIKE_LEVEL 1000000              
   86 #define CLK_CHANGED_LEVEL 1000 
   88 #define LARGE_SPIKE 80                       
   89 #define NOISE_ACCUM_RATE 0.1                 
   90 #define NOISE_LEVEL_MIN 4                    
   94 #define MAX_LINE_LEN 50 
   95 #define STRBUF_SZ 1000 
   96 #define LOGBUF_SZ 1000 
   97 #define MSGBUF_SZ 1000 
   98 #define NIST_MSG_SZ 200 
   99 #define CONFIG_FILE_SZ 10000 
  102 #define ERROR_DISTRIB_LEN 121 
  103 #define JITTER_DISTRIB_LEN 181 
  104 #define INTRPT_DISTRIB_LEN 121 
  106 #define HARD_LIMIT_NONE 32768 
  107 #define HARD_LIMIT_1024 1024 
  108 #define HARD_LIMIT_4 4 
  109 #define HARD_LIMIT_1 1 
  114 #define MAX_CONFIGS 32 
  116 #define ERROR_DISTRIB 1             // Configuration file Keys 
  117 #define ALERT_PPS_LOST 2 
  118 #define JITTER_DISTRIB 4 
  119 #define EXIT_LOST_PPS 8 
  121 #define OUTPUT_GPIO 32 
  122 #define INTRPT_GPIO 64 
  125 #define SERIAL_PORT 512 
  127 #define SERVICEDIR 2048 
  128 #define CONFIGDIR 4096 
  133 #define LOGDIR 131072 
  134 #define PPSDELAY 262144 
  135 #define MODULEDIR 524288 
  136 #define PPSDEVICE 1048576 
  137 #define PPSPHASE 2097152 
  138 #define PROCDIR 4194304 
  139 #define SEGREGATE 8388608 
  250     int startingFromRestore;
 
  262     int blockDetectClockChange;
 
  270     unsigned int config_select;
 
  278     double lastFreqOffset;
 
  279     double freqOffsetSum;
 
  282     unsigned int lastActiveCount;
 
  313 time_t getServerTime(
const char *, 
int, 
char *, 
char *);
 
  326 struct saveFileData {
 
  329     const char *filename;       
 
  350     char pps_device[100];
 
  351     char module_file[100];
 
  376     void insert(
int idx, 
int val){
 
  377         for (
int i = ln; i >= idx; i--){
 
  389         for (
int i = 0; i < size; i++){
 
  399         lst = 
new intPair[size];
 
  408     int binaryInsert(
int val);
 
  409     double averageBelow(
int maxVal);
 
  413 void initSerialLocalData(
void);
 
  425 int disableNTP(
void);
 
  428 int read_logerr(
int fd, 
char *, 
int, 
const char *);
 
  429 void writeInterruptDistribFile(
void);
 
  433 void writeSysdelayDistribFile(
void);
 
  441 void buildInterruptJitterDistrib(
int);
 
  445 int getTimeErrorOverSerial(
int *);
 
  
bool slewIsLow
Set to "true" in getAcquireState() when G.avgSlew is less than SLEW_MAX. This is a precondition for g...
double rawErrorDistrib[ERROR_DISTRIB_LEN]
The distribution of rawError values accumulated in buildRawErrorDistrib().
double integral[NUM_INTEGRALS]
Array of integrals constructed by makeAverageIntegral().
#define OFFSETFIFO_LEN
Length of G.correctionFifo which contains the data used to generate G.avgCorrection.
int correctionAccum
Accumulates G.timeCorrection values from G.correctionFifo in getMovingAverage() in order to generate ...
char * logbuf
Space for returned log messages.
char * serialPort
The serial port filename when serial time is used.
int hardLimit
An adaptive limit value determined by setHardLimit() and applied to G.rawError by clampJitter() as th...
const char * cpuinfo_file
int accessDaemon(int argc, char *argv[])
unsigned int activeCount
Advancing count of active (not skipped) controller cycles once G.isControlling is "true".
void recordFrequencyVars(void)
int correctionFifoCount
Signals that G.correctionFifo contains a full count of G.timeCorrection values.
int serverIndex
Identifying index from the list of active NIST servers.
void freeSerialThread(timeCheckParams *tcp)
int invProportionalGain
Controller proportional gain configured inversely to use as an int divisor.
double freqOffset
System clock frequency correction calculated as G.integralTimeCorrection * G.integralGain.
#define NUM_INTEGRALS
Number of integrals used by makeAverageIntegral() to calculate the one minute clock frequency correct...
int sysCommand(const char *cmd)
int read_logerr(int fd, char *buf, int sz, const char *filename)
void freeNISTThreads(timeCheckParams *tcp)
pthread_attr_t attr
Thread attribute object.
void writeToLog(char *logbuf, const char *location)
void writeTimestamp(double timestamp)
int allocInitializeNISTThreads(timeCheckParams *tcp)
pthread_t * tid
Thread id.
int interruptLossCount
Records the number of consecutive lost PPS interrupt times.
int timeCorrection
Time correction value constructed in makeTimeCorrection().
unsigned int seq_num
Advancing count of the number of PPS interrupt timings that have been received.
int find_source(const char *path, pps_handle_t *handle, int *avail_mode)
const char * displayParams_file
Temporary file storing params for the status display.
int zeroError
The controller error resulting from removing jitter noise from G.rawError in removeNoise().
int ppsPhase
Accounts for a possible hardware inversion of the PPS signal.
int nDelaySpikes
Current count of continuous delay spikes made by detectDelaySpike().
int makeSerialTimeQuery(timeCheckParams *tcp)
double integralTimeCorrection
Integral or average integral of G.timeCorrection returned by getIntegral();.
double slewAccum
Accumulates G.rawError in getTimeSlew() and is used to determine G.avgSlew.
const char * gmtTime_file
double t_mono_last
Last recorded monotonic time.
void buildJitterDistrib(int rawError)
void saveGPSTime(timeCheckParams *tcp)
int consensusTimeError
Consensus value of whole-second time corrections for DST or leap seconds from Internet NIST servers.
int assignProcessorAffinity(void)
int correctionFifo_idx
Advances G.correctionFifo on each controller cycle in integralIsReady() which returns "true" every 60...
void makeNISTTimeQuery(timeCheckParams *tcp)
unsigned int ppsCount
Advancing count of G.rawErrorDistrib[] entries made by buildRawErrorDistrib().
bool clampAbsolute
Hard limit relative to zero if true else relative to average G.rawError.
#define ERROR_DISTRIB_LEN
int t_count
Rounded seconds counted at the time of G.t_now.
int writeFileMsgToLogbuf(const char *filename, char *logbuf)
bool doReadSerial
Flag to read serial messages from serial port.
int integralCount
Counts the integrals formed over the last 10 controller cycles and signals when all integrals in G....
const char * distrib_file
Stores a forming distribution of offset corrections.
int readPPSTimestamp(pps_handle_t *handle, int *avail_mode, int *tm)
bool * threadIsBusy
True while thread is waiting for or processing a time query.
int writeFileMsgToLog(const char *filename)
const char * pps_msg_file
void writeOffsets(const char *filename)
char * buf
Space for the active NIST server list.
const char * jitter_distrib_file
Stores a forming distribution of offset corrections.
void bufferStatusMsg(const char *msg)
const char * arrayData_file
Stores a request sent to the PPS-Client daemon.
double noiseLevel
PPS time delay value beyond which a delay is defined to be a delay spike.
bool interruptReceived
Set "true" when makeTimeCorrection() processes an interrupt time from the Linux PPS device driver.
#define FREQDIFF_INTRVL
The number of minutes between Allan deviation samples of system clock frequency correction.
char linuxVersion[20]
Array for recording the Linux version.
const char * last_jitter_distrib_file
Stores the completed distribution of offset corrections.
#define NUM_5_MIN_INTERVALS
Number of five minute intervals in 24 hours.
int slewAccum_cnt
Count of the number of times G.rawError has been summed into G.slewAccum.
double t_mono_now
Current monotonic time.
int cpuVersion
The principle CPU version number for Raspberry Pi processors else 0.
int t_now
Rounded seconds of current time reported by gettimeofday().
struct timex t3
Passes G.timeCorrection to the system function adjtimex() in makeTimeCorrection().
double avgSlew
Average slew value determined by getTimeSlew() from the average of G.slewAccum each time G....
char * strbuf
Space for messages and query strings.
bool clockChanged
Set true if an external clock change is detected.
int minSustainedDelay
The observed minimum delay value of a sustained sequence of delay spikes.
int nCores
If PPS-Client is segregated, identifies the number of processor cores.
const char * nistTime_file
struct timespec setSyncDelay(int timeAt, int fracSec)
double avgCorrection
A one-minute rolling average of G.timeCorrection values generated by getMovingAverage().
#define JITTER_DISTRIB_LEN
int open_logerr(const char *filename, int flags, const char *location)
const char * integral_state_file
const char * log_file
Stores activity and errors.
int bufferStateParams(void)
char ** ntp_server
The active NIST server list when NIST is used.
void TERMhandler(int sig)
double integralGain
Current controller integral gain.
void recordOffsets(int timeCorrection)
const char * config_file
The PPS-Client configuration file.
bool configWasRead
True if pps-client.conf was read at least once.
int writeStatusStrings(void)
void buildErrorDistrib(int timeCorrection)
bool interruptLost
Set "true" when a PPS interrupt time fails to be received.
const char * assert_file
The timestamps of the time corrections each second.
int tm[6]
Returns the timestamp from the Linux PPS device driver as a pair of ints.
void writeToLogNoTimestamp(char *logbuf)
const char * pidFilename
Stores the PID of PPS-Client.
bool isDelaySpike
Set "true" by detectDelaySpike() when G.rawError exceeds G.noiseLevel.
int allocInitializeSerialThread(timeCheckParams *tcp)
int ppsTimestamp
Fractional second value of the PPS timestamp from the kernel driver.
Struct for program-wide global variables showing those important to the controller.
#define INTRPT_DISTRIB_LEN
const char * old_log_file
Stores activity and errors.
const char * last_distrib_file
Stores the completed distribution of offset corrections.
void showStatusEachSecond(void)
int rawError
Signed difference: G.ppsTimestamp - G.zeroOffset in makeTimeCorrection().
double avgIntegral
One-minute average of the integrals in G.integral[].
bool isControlling
Set "true" by getAcquireState() when the control loop can begin to control the system clock frequency...
int correctionFifo[OFFSETFIFO_LEN]
Contains the G.timeCorrection values from over the previous 60 seconds.
struct timeval t
Time of system response to the PPS interrupt received from the Linux PPS device driver.
int * serverTimeDiff
Time difference between local time and server time.
int serialTimeError
Error reported by GPS serial port.S.
bool isVerbose
Enables continuous printing of PPS-Client status params when "true".
const char * linuxVersion_file
int useCore
If PPS-Client is segregated, the core on which it runs.