This is Unofficial EPICS BASE Doxygen Site
osdTime.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2015 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 #define _VSB_CONFIG_FILE <../lib/h/config/vsbConfig.h>
11 
12 #include <vxWorks.h>
13 #include <sntpcLib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <taskLib.h>
18 
19 #define EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
20 #include "epicsTime.h"
21 #include "osiNTPTime.h"
22 #include "osiClockTime.h"
23 #include "generalTimeSup.h"
24 #include "envDefs.h"
25 
26 #define NTP_REQUEST_TIMEOUT 4 /* seconds */
27 
28 extern "C" {
29  int tz2timezone(void);
30 }
31 
32 static char sntp_sync_task[] = "ipsntps";
33 static char ntp_daemon[] = "ipntpd";
34 
35 static const char *pserverAddr = NULL;
36 static CLOCKTIME_SYNCHOOK prevHook;
37 
38 extern char* sysBootLine;
39 
40 static void timeSync(int synchronized) {
41  if (!tz2timezone())
42  ClockTime_syncHook = prevHook; /* Don't call me again */
43 }
44 
45 static int timeRegister(void)
46 {
47  /* If TZ not defined, set it from EPICS_TZ */
48  if (getenv("TZ") == NULL) {
49  const char *tz = envGetConfigParamPtr(&EPICS_TZ);
50 
51  if (tz && *tz) {
52  epicsEnvSet("TZ", tz);
53 
54  /* Call tz2timezone() once we know what year it is */
55  prevHook = ClockTime_syncHook;
56  ClockTime_syncHook = timeSync;
57  }
58  else if (getenv("TIMEZONE") == NULL)
59  printf("timeRegister: No Time Zone Information available\n");
60  }
61 
62  // Define EPICS_TS_FORCE_NTPTIME to force use of NTPTime provider
63  bool useNTP = getenv("EPICS_TS_FORCE_NTPTIME") != NULL;
64 
65  if (!useNTP &&
66  (taskNameToId(sntp_sync_task) != ERROR ||
67  taskNameToId(ntp_daemon) != ERROR)) {
68  // A VxWorks 6 SNTP/NTP sync task is running
69  struct timespec clockNow;
70 
71  useNTP = clock_gettime(CLOCK_REALTIME, &clockNow) != OK ||
72  clockNow.tv_sec < BUILD_TIME;
73  // Assumes VxWorks and the host OS have the same epoch
74  }
75 
76  if (useNTP) {
77  // Start NTP first so it can be used to sync ClockTime
78  NTPTime_Init(100);
80  } else {
82  }
84  return 1;
85 }
86 static int done = timeRegister();
87 
88 
89 // Routines for NTPTime provider
90 
91 int osdNTPGet(struct timespec *ts)
92 {
93  return sntpcTimeGet(const_cast<char *>(pserverAddr),
95 }
96 
97 void osdNTPInit(void)
98 {
100  if (!pserverAddr) { /* use the boot host */
101  BOOT_PARAMS bootParms;
102  static char host_addr[BOOT_ADDR_LEN];
103 
104  bootStringToStruct(sysBootLine, &bootParms);
105  /* bootParms.had = host IP address */
106  strncpy(host_addr, bootParms.had, BOOT_ADDR_LEN);
107  pserverAddr = host_addr;
108  }
109 }
110 
111 void osdNTPReport(void)
112 {
113  if (pserverAddr)
114  printf("NTP Server = %s\n", pserverAddr);
115 }
116 
117 
118 // Other Time Routines
119 
120 // vxWorks localtime_r returns different things in different versions.
121 // It can't fail though, so we just ignore the return value.
122 int epicsTime_localtime(const time_t *clock, struct tm *result)
123 {
124  localtime_r(clock, result);
125  return epicsTimeOK;
126 }
127 
128 // vxWorks gmtime_r returns different things in different versions.
129 // It can't fail though, so we just ignore the return value.
130 int epicsTime_gmtime ( const time_t *pAnsiTime, struct tm *pTM )
131 {
132  gmtime_r(pAnsiTime, pTM);
133  return epicsTimeOK;
134 }
int sysClkRateGet(void)
pvac::PutEvent result
Definition: clientSync.cpp:117
LIBCOM_API const ENV_PARAM EPICS_TZ
int osdNTPGet(struct timespec *ts)
Definition: osdTime.cpp:45
void ClockTime_Init(int synchronize)
void osdNTPReport(void)
Definition: osdTime.cpp:101
LIBCOM_API const char *epicsStdCall envGetConfigParamPtr(const ENV_PARAM *pParam)
Get a configuration parameter&#39;s value or default string.
Definition: envSubr.c:81
Routines to get and set EPICS environment parameters.
#define printf
Definition: epicsStdio.h:41
void osdMonotonicInit(void)
Definition: osdMonotonic.c:19
#define NULL
Definition: catime.c:38
#define epicsTimeOK
Success.
Definition: epicsTime.h:339
void NTPTime_Init(int priority)
Definition: osiNTPTime.c:121
time_t tv_sec
Definition: osdTime.h:22
LIBCOM_API void epicsStdCall epicsEnvSet(const char *name, const char *value)
Set an environment variable&#39;s value.
Definition: osdEnv.c:35
LIBCOM_API const ENV_PARAM EPICS_TS_NTP_INET
#define CLOCKTIME_SYNC
Definition: osiClockTime.h:12
#define CLOCKTIME_NOSYNC
Definition: osiClockTime.h:11
void osdNTPInit(void)
Definition: osdTime.cpp:81
int tz2timezone(void)
Definition: tz2timezone.c:158
Defined by POSIX Real Time.
Definition: osdTime.h:21
EPICS time-stamps (epicsTimeStamp), epicsTime C++ class and C functions for handling wall-clock times...
#define NTP_REQUEST_TIMEOUT
Definition: osdTime.cpp:26
char * sysBootLine
int epicsTime_gmtime(const time_t *pAnsiTime, struct tm *pTM)
Break down a time_t into a struct tm in the UTC timezone.
Definition: osdTime.cpp:55
int epicsTime_localtime(const time_t *clock, struct tm *result)
Break down a time_t into a struct tm in the local timezone.
Definition: osdTime.cpp:61