Commit c842f754 authored by Alessandro Rubini's avatar Alessandro Rubini

support UTC/TAI offset

This adds support for the utc/tai offset.
Unix, bare and wrs use UTC in system time and TAI in PTP frames.
WR time is TAI as well, and wrpc-sw passes 0 as offset value when
run as master.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 3b6b78d3
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/timex.h>
#include <ppsi/ppsi.h> #include <ppsi/ppsi.h>
#include "ppsi-unix.h" #include "ppsi-unix.h"
...@@ -35,6 +36,7 @@ int main(int argc, char **argv) ...@@ -35,6 +36,7 @@ int main(int argc, char **argv)
{ {
struct pp_globals *ppg; struct pp_globals *ppg;
struct pp_instance *ppi; struct pp_instance *ppi;
struct timex t;
int i; int i;
setbuf(stdout, NULL); setbuf(stdout, NULL);
...@@ -55,6 +57,10 @@ int main(int argc, char **argv) ...@@ -55,6 +57,10 @@ int main(int argc, char **argv)
if ((!ppg->arch_data) || (!ppg->pp_instances)) if ((!ppg->arch_data) || (!ppg->pp_instances))
exit(__LINE__); exit(__LINE__);
/* Set offset here, so config parsing can override it */
if (adjtimex(&t) >= 0)
timePropertiesDS.currentUtcOffset = t.tai;
pp_config_file(ppg, &argc, argv, NULL, "link 0\niface eth0\n" pp_config_file(ppg, &argc, argv, NULL, "link 0\niface eth0\n"
"proto udp\n" /* mandatory trailing \n */); "proto udp\n" /* mandatory trailing \n */);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/timex.h>
#include <minipc.h> #include <minipc.h>
#include <hal_exports.h> #include <hal_exports.h>
...@@ -42,6 +43,7 @@ int main(int argc, char **argv) ...@@ -42,6 +43,7 @@ int main(int argc, char **argv)
{ {
struct pp_globals *ppg; struct pp_globals *ppg;
struct pp_instance *ppi; struct pp_instance *ppi;
struct timex t;
int i; int i;
setbuf(stdout, NULL); setbuf(stdout, NULL);
...@@ -76,6 +78,18 @@ int main(int argc, char **argv) ...@@ -76,6 +78,18 @@ int main(int argc, char **argv)
if ((!ppg->arch_data) || (!ppg->pp_instances)) if ((!ppg->arch_data) || (!ppg->pp_instances))
exit(__LINE__); exit(__LINE__);
/* Set offset here, so config parsing can override it */
if (adjtimex(&t) >= 0) {
int *p;
/*
* Our WRS kernel has tai support, but our compiler does not.
* We are 32-bit only, and we know for sure that tai is
* exactly after stbcnt. It's a bad hack, but it works
*/
p = (int *)(&t.stbcnt) + 1;
timePropertiesDS.currentUtcOffset = *p;
}
pp_config_file(ppg, &argc, argv, "/wr/etc/ppsi.conf", pp_config_file(ppg, &argc, argv, "/wr/etc/ppsi.conf",
"link 0\niface wr0\n" /* mandatory trailing \n */); "link 0\niface wr0\n" /* mandatory trailing \n */);
......
...@@ -38,6 +38,7 @@ int ppsi_main(int argc, char **argv) ...@@ -38,6 +38,7 @@ int ppsi_main(int argc, char **argv)
{ {
struct pp_globals *ppg = &ppg_static; struct pp_globals *ppg = &ppg_static;
struct pp_instance *ppi = &ppi_static; /* no malloc, one instance */ struct pp_instance *ppi = &ppi_static; /* no malloc, one instance */
struct bare_timex t;
if (pp_diag_verbosity) if (pp_diag_verbosity)
pp_printf("ppsi starting. Built on %s\n", __DATE__); pp_printf("ppsi starting. Built on %s\n", __DATE__);
...@@ -58,6 +59,9 @@ int ppsi_main(int argc, char **argv) ...@@ -58,6 +59,9 @@ int ppsi_main(int argc, char **argv)
ppg->rt_opts = &default_rt_opts; ppg->rt_opts = &default_rt_opts;
if (sys_adjtimex(&t) >= 0)
timePropertiesDS.currentUtcOffset = t.tai;
if (pp_parse_cmdline(ppg, argc, argv) != 0) if (pp_parse_cmdline(ppg, argc, argv) != 0)
return -1; return -1;
......
...@@ -61,6 +61,9 @@ static void s1(struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann) ...@@ -61,6 +61,9 @@ static void s1(struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann)
/* Timeproperties DS */ /* Timeproperties DS */
prop->timeSource = ann->timeSource; prop->timeSource = ann->timeSource;
if (prop->currentUtcOffset != ann->currentUtcOffset)
pp_diag(ppi, bmc, 1, "New UTC offset: %i\n",
ann->currentUtcOffset);
prop->currentUtcOffset = ann->currentUtcOffset; prop->currentUtcOffset = ann->currentUtcOffset;
/* FIXME: can't we just copy the bit keeping values? */ /* FIXME: can't we just copy the bit keeping values? */
......
...@@ -16,7 +16,8 @@ static int bare_time_get(struct pp_instance *ppi, TimeInternal *t) ...@@ -16,7 +16,8 @@ static int bare_time_get(struct pp_instance *ppi, TimeInternal *t)
pp_error("%s:", __func__); pp_error("%s:", __func__);
sys_exit(1); sys_exit(1);
} }
t->seconds = tv.tv_sec; /* TAI = UTC + 34 */
t->seconds = tv.tv_sec + DSPRO(ppi)->currentUtcOffset;
t->nanoseconds = tv.tv_usec * 1000; t->nanoseconds = tv.tv_usec * 1000;
if (!(pp_global_flags & PP_FLAG_NOTIMELOG)) if (!(pp_global_flags & PP_FLAG_NOTIMELOG))
pp_diag(ppi, time, 2, "%s: %9li.%06li\n", __func__, pp_diag(ppi, time, 2, "%s: %9li.%06li\n", __func__,
...@@ -28,7 +29,8 @@ static int bare_time_set(struct pp_instance *ppi, TimeInternal *t) ...@@ -28,7 +29,8 @@ static int bare_time_set(struct pp_instance *ppi, TimeInternal *t)
{ {
struct bare_timeval tv; struct bare_timeval tv;
tv.tv_sec = t->seconds; /* UTC = TAI - 34 */
tv.tv_sec = t->seconds - DSPRO(ppi)->currentUtcOffset;
tv.tv_usec = t->nanoseconds / 1000; tv.tv_usec = t->nanoseconds / 1000;
if (sys_settimeofday(&tv, NULL) < 0) { if (sys_settimeofday(&tv, NULL) < 0) {
......
...@@ -24,7 +24,8 @@ static int unix_time_get(struct pp_instance *ppi, TimeInternal *t) ...@@ -24,7 +24,8 @@ static int unix_time_get(struct pp_instance *ppi, TimeInternal *t)
struct timespec tp; struct timespec tp;
if (clock_gettime(CLOCK_REALTIME, &tp) < 0) if (clock_gettime(CLOCK_REALTIME, &tp) < 0)
clock_fatal_error("clock_gettime"); clock_fatal_error("clock_gettime");
t->seconds = tp.tv_sec; /* TAI = UTC + 34 */
t->seconds = tp.tv_sec + DSPRO(ppi)->currentUtcOffset;
t->nanoseconds = tp.tv_nsec; t->nanoseconds = tp.tv_nsec;
t->correct = 1; t->correct = 1;
if (!(pp_global_flags & PP_FLAG_NOTIMELOG)) if (!(pp_global_flags & PP_FLAG_NOTIMELOG))
...@@ -37,7 +38,8 @@ static int32_t unix_time_set(struct pp_instance *ppi, TimeInternal *t) ...@@ -37,7 +38,8 @@ static int32_t unix_time_set(struct pp_instance *ppi, TimeInternal *t)
{ {
struct timespec tp; struct timespec tp;
tp.tv_sec = t->seconds; /* UTC = TAI - 34 */
tp.tv_sec = t->seconds - DSPRO(ppi)->currentUtcOffset;
tp.tv_nsec = t->nanoseconds; tp.tv_nsec = t->nanoseconds;
if (clock_settime(CLOCK_REALTIME, &tp) < 0) if (clock_settime(CLOCK_REALTIME, &tp) < 0)
clock_fatal_error("clock_settime"); clock_fatal_error("clock_settime");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment