#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
#include "ntp_stdlib.h"
#include "ntpd.h"
#if defined(HAVE_TERMIOS_H) || defined(STREAM)
#include <termios.h>
#define TTY_GETATTR(_FD_, _ARG_) tcgetattr((_FD_), (_ARG_))
#define TTY_SETATTR(_FD_, _ARG_) tcsetattr((_FD_), TCSANOW, (_ARG_))
#else
# if defined(HAVE_TERMIO_H) || defined(HAVE_SYSV_TTYS)
# include <termio.h>
# define TTY_GETATTR(_FD_, _ARG_) ioctl((_FD_), TCGETA, (_ARG_))
# define TTY_SETATTR(_FD_, _ARG_) ioctl((_FD_), TCSETAW, (_ARG_))
# endif/* HAVE_TERMIO_H || HAVE_SYSV_TTYS */
#endif
#ifndef TTY_GETATTR
#include "Bletch: MUST DEFINE ONE OF 'HAVE_TERMIOS_H' or 'HAVE_TERMIO_H'"
#endif
#ifndef days_per_year
#define days_per_year(_x_) (((_x_) % 4) ? 365 : (((_x_) % 400) ? 365 : 366))
#endif
#define timernormalize(_a_) \
if ((_a_)->tv_usec >= 1000000) \
{ \
(_a_)->tv_sec += (_a_)->tv_usec / 1000000; \
(_a_)->tv_usec = (_a_)->tv_usec % 1000000; \
} \
if ((_a_)->tv_usec < 0) \
{ \
(_a_)->tv_sec -= 1 + (-(_a_)->tv_usec / 1000000); \
(_a_)->tv_usec = 999999 - (-(_a_)->tv_usec - 1); \
}
#ifdef timeradd
#undef timeradd
#endif
#define timeradd(_a_, _b_) \
(_a_)->tv_sec += (_b_)->tv_sec; \
(_a_)->tv_usec += (_b_)->tv_usec; \
timernormalize((_a_))
#ifdef timersub
#undef timersub
#endif
#define timersub(_a_, _b_) \
(_a_)->tv_sec -= (_b_)->tv_sec; \
(_a_)->tv_usec -= (_b_)->tv_usec; \
timernormalize((_a_))
#define PRINTF if (interactive) printf
#define LPRINTF if (interactive && loop_filter_debug) printf
#ifdef DEBUG
#define dprintf(_x_) LPRINTF _x_
#else
#define dprintf(_x_)
#endif
#ifdef DECL_ERRNO
extern int errno;
#endif
static char *revision = "4.18";
static int interactive = 0;
static int loop_filter_debug = 0;
static int no_set = 0;
#define DEFAULT_DELAY 230000
#define NO_SYNC 0x01
#define SYNC 0x02
static int sync_state = NO_SYNC;
static time_t last_sync;
static unsigned long ticks = 0;
static char pat[] = "-\\|/";
#define LINES (24-2)
#define MAX_UNSYNC (10*60)
#define NOTICE_INTERVAL (20*60)
#define USECSCALE 10
#define TIMECONSTANT 2
#define ADJINTERVAL 0
#define FREQ_WEIGHT 18
#define PHASE_WEIGHT 7
#define MAX_DRIFT 0x3FFFFFFF
#define R_SHIFT(_X_, _Y_) (((_X_) < 0) ? -(-(_X_) >> (_Y_)) : ((_X_) >> (_Y_)))
static struct timeval max_adj_offset = { 0, 128000 };
static long clock_adjust = 0;
static long accum_drift = 0;
static long adjustments = 0;
static char skip_adjust = 1;
#define DCFB_ANNOUNCE 0x0001
#define DCFB_DST 0x0002
#define DCFB_LEAP 0x0004
#define DCFB_ALTERNATE 0x0008
struct clocktime
{
long wday;
long day;
long month;
long year;
long hour;
long minute;
long second;
long usecond;
long utcoffset;
long flags;
};
typedef struct clocktime clocktime_t;
#define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
#define TIMES24(_X_) (((_X_) << 4) + ((_X_) << 3))
#define TIMES60(_X_) ((((_X_) << 4) - (_X_)) << 2)
#define l_abs(_x_) (((_x_) < 0) ? -(_x_) : (_x_))
#define CVT_MASK 0x0000000F
#define CVT_NONE 0x00000001
#define CVT_FAIL 0x00000002
#define CVT_OK 0x00000004
#define CVT_BADFMT 0x00000010
#define CVT_BADDATE 0x00000020
#define CVT_BADTIME 0x00000040
static struct rawdcfcode
{
char offset;
} rawdcfcode[] =
{
{ 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
{ 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
};
#define DCF_M 0
#define DCF_R 1
#define DCF_A1 2
#define DCF_Z 3
#define DCF_A2 4
#define DCF_S 5
#define DCF_M1 6
#define DCF_M10 7
#define DCF_P1 8
#define DCF_H1 9
#define DCF_H10 10
#define DCF_P2 11
#define DCF_D1 12
#define DCF_D10 13
#define DCF_DW 14
#define DCF_MO 15
#define DCF_MO0 16
#define DCF_Y1 17
#define DCF_Y10 18
#define DCF_P3 19
static struct partab
{
char offset;
} partab[] =
{
{ 21 }, { 29 }, { 36 }, { 59 }
};
#define DCF_P_P1 0
#define DCF_P_P2 1
#define DCF_P_P3 2
#define DCF_Z_MET 0x2
#define DCF_Z_MED 0x1
static struct dcfparam
{
unsigned char onebits[60];
unsigned char zerobits[60];
} dcfparam =
{
"###############RADMLS1248124P124812P1248121241248112481248P",
"--------------------s-------p------p----------------------p"
};
static unsigned long
ext_bf(
register unsigned char *buf,
register int idx
)
{
register unsigned long sum = 0;
register int i, first;
first = rawdcfcode[idx].offset;
for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
{
sum <<= 1;
sum |= (buf[i] != dcfparam.zerobits[i]);
}
return sum;
}
static unsigned
pcheck(
register unsigned char *buf,
register int idx
)
{
register int i,last;
register unsigned psum = 1;
last = partab[idx+1].offset;
for (i = partab[idx].offset; i < last; i++)
psum ^= (buf[i] != dcfparam.zerobits[i]);
return psum;
}
static unsigned long
convert_rawdcf(
unsigned char *buffer,
int size,
clocktime_t *clock_time
)
{
if (size < 57)
{
PRINTF("%-30s", "*** INCOMPLETE");
return CVT_NONE;
}
if ((ext_bf(buffer, DCF_S) == 1) &&
pcheck(buffer, DCF_P_P1) &&
pcheck(buffer, DCF_P_P2) &&
pcheck(buffer, DCF_P_P3))
{
clock_time->flags = 0;
clock_time->usecond= 0;
clock_time->second = 0;
clock_time->minute = ext_bf(buffer, DCF_M10);
clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
clock_time->hour = ext_bf(buffer, DCF_H10);
clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
clock_time->day = ext_bf(buffer, DCF_D10);
clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
clock_time->month = ext_bf(buffer, DCF_MO0);
clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
clock_time->year = ext_bf(buffer, DCF_Y10);
clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
clock_time->wday = ext_bf(buffer, DCF_DW);
switch (ext_bf(buffer, DCF_Z))
{
case DCF_Z_MET:
clock_time->utcoffset = -60;
break;
case DCF_Z_MED:
clock_time->flags |= DCFB_DST;
clock_time->utcoffset = -120;
break;
default:
PRINTF("%-30s", "*** BAD TIME ZONE");
return CVT_FAIL|CVT_BADFMT;
}
if (ext_bf(buffer, DCF_A1))
clock_time->flags |= DCFB_ANNOUNCE;
if (ext_bf(buffer, DCF_A2))
clock_time->flags |= DCFB_LEAP;
if (ext_bf(buffer, DCF_R))
clock_time->flags |= DCFB_ALTERNATE;
return CVT_OK;
}
else
{
PRINTF("%-30s", "*** BAD FORMAT (invalid/parity)");
return CVT_FAIL|CVT_BADFMT;
}
}
static unsigned long
cvt_rawdcf(
unsigned char *buffer,
int size,
clocktime_t *clock_time
)
{
register unsigned char *s = buffer;
register unsigned char *e = buffer + size;
register unsigned char *b = dcfparam.onebits;
register unsigned char *c = dcfparam.zerobits;
register unsigned rtc = CVT_NONE;
register unsigned int i, lowmax, highmax, cutoff, span;
#define BITS 9
unsigned char histbuf[BITS];
for (i = 0; i < BITS; i++)
{
histbuf[i] = 0;
}
cutoff = 0;
lowmax = 0;
while (s < e)
{
register unsigned int ch = *s ^ 0xFF;
if (!((ch+1) & ch) || !*s)
{
for (i = 0; ch; i++)
{
ch >>= 1;
}
*s = i;
histbuf[i]++;
cutoff += i;
lowmax++;
}
else
{
dprintf(("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, s - buffer));
*s = (unsigned char)~0;
rtc = CVT_FAIL|CVT_BADFMT;
}
s++;
}
if (lowmax)
{
cutoff /= lowmax;
}
else
{
cutoff = 4;
}
dprintf(("parse: cvt_rawdcf: average bit count: %d\n", cutoff));
lowmax = 0;
highmax = 0;
dprintf(("parse: cvt_rawdcf: histogram:"));
for (i = 0; i <= cutoff; i++)
{
lowmax += histbuf[i] * i;
highmax += histbuf[i];
dprintf((" %d", histbuf[i]));
}
dprintf((" <M>"));
lowmax += highmax / 2;
if (highmax)
{
lowmax /= highmax;
}
else
{
lowmax = 0;
}
highmax = 0;
cutoff = 0;
for (; i < BITS; i++)
{
highmax+=histbuf[i] * i;
cutoff +=histbuf[i];
dprintf((" %d", histbuf[i]));
}
dprintf(("\n"));
if (cutoff)
{
highmax /= cutoff;
}
else
{
highmax = BITS-1;
}
span = cutoff = lowmax;
for (i = lowmax; i <= highmax; i++)
{
if (histbuf[cutoff] > histbuf[i])
{
cutoff = span = i;
}
else
if (histbuf[cutoff] == histbuf[i])
{
span = i;
}
}
cutoff = (cutoff + span) / 2;
dprintf(("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff));
s = buffer;
while ((s < e) && *c && *b)
{
if (*s == (unsigned char)~0)
{
*s = '?';
}
else
{
*s = (*s >= cutoff) ? *b : *c;
}
s++;
b++;
c++;
}
if (rtc != CVT_NONE)
{
PRINTF("%-30s", "*** BAD DATA");
}
return (rtc == CVT_NONE) ? convert_rawdcf(buffer, size, clock_time) : rtc;
}
static time_t
dcf_to_unixtime(
clocktime_t *clock_time,
unsigned *cvtrtc
)
{
#define SETRTC(_X_) { if (cvtrtc) *cvtrtc = (_X_); }
static int days_of_month[] =
{
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
register int i;
time_t t;
if ( clock_time->year < YEAR_PIVOT )
clock_time->year += 100;
if ( clock_time->year < YEAR_BREAK )
clock_time->year += 1900;
if (clock_time->year < (YEAR_PIVOT + 1900) )
{
SETRTC(CVT_FAIL|CVT_BADDATE);
return -1;
}
t = julian0( clock_time->year ) - julian0( 1970 );
if (clock_time->month <= 0 || clock_time->month > 12)
{
SETRTC(CVT_FAIL|CVT_BADDATE);
return -1;
}
#if 0
if (clock_time->month < 3 && days_per_year(clock_time->year) == 366)
t--;
#endif
for (i = 1; i < clock_time->month; i++)
{
t += days_of_month[i];
}
if (clock_time->day < 1 || ((clock_time->month == 2 && days_per_year(clock_time->year) == 366) ?
clock_time->day > 29 : clock_time->day > days_of_month[clock_time->month]))
{
SETRTC(CVT_FAIL|CVT_BADDATE);
return -1;
}
t += clock_time->day - 1;
if (clock_time->hour < 0 || clock_time->hour >= 24)
{
SETRTC(CVT_FAIL|CVT_BADTIME);
return -1;
}
t = TIMES24(t) + clock_time->hour;
if (clock_time->minute < 0 || clock_time->minute > 59)
{
SETRTC(CVT_FAIL|CVT_BADTIME);
return -1;
}
t = TIMES60(t) + clock_time->minute;
t += clock_time->utcoffset;
if (clock_time->second < 0 || clock_time->second > 60)
{
SETRTC(CVT_FAIL|CVT_BADTIME);
return -1;
}
t = TIMES60(t) + clock_time->second;
return t;
}
static char
type(
unsigned int c
)
{
c ^= 0xFF;
return (c > 0xF);
}
static const char *wday[8] =
{
"??",
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa",
"Su"
};
static char *
pr_timeval(
struct timeval *val
)
{
static char buf[20];
if (val->tv_sec == 0)
sprintf(buf, "%c0.%06ld", (val->tv_usec < 0) ? '-' : '+', (long int)l_abs(val->tv_usec));
else
sprintf(buf, "%ld.%06ld", (long int)val->tv_sec, (long int)l_abs(val->tv_usec));
return buf;
}
static void
set_time(
struct timeval *offset
)
{
struct timeval the_time;
if (no_set)
return;
LPRINTF("set_time: %s ", pr_timeval(offset));
syslog(LOG_NOTICE, "setting time (offset %s)", pr_timeval(offset));
if (gettimeofday(&the_time, 0L) == -1)
{
perror("gettimeofday()");
}
else
{
timeradd(&the_time, offset);
if (settimeofday(&the_time, 0L) == -1)
{
perror("settimeofday()");
}
}
}
static void
adj_time(
long offset
)
{
struct timeval time_offset;
if (no_set)
return;
time_offset.tv_sec = offset / 1000000;
time_offset.tv_usec = offset % 1000000;
LPRINTF("adj_time: %ld us ", (long int)offset);
if (adjtime(&time_offset, 0L) == -1)
perror("adjtime()");
}
static void
read_drift(
const char *drift_file
)
{
FILE *df;
df = fopen(drift_file, "r");
if (df != NULL)
{
int idrift = 0, fdrift = 0;
fscanf(df, "%4d.%03d", &idrift, &fdrift);
fclose(df);
LPRINTF("read_drift: %d.%03d ppm ", idrift, fdrift);
accum_drift = idrift << USECSCALE;
fdrift = (fdrift << USECSCALE) / 1000;
accum_drift += fdrift & (1<<USECSCALE);
LPRINTF("read_drift: drift_comp %ld ", (long int)accum_drift);
}
}
static void
update_drift(
const char *drift_file,
long offset,
time_t reftime
)
{
FILE *df;
df = fopen(drift_file, "w");
if (df != NULL)
{
int idrift = R_SHIFT(accum_drift, USECSCALE);
int fdrift = accum_drift & ((1<<USECSCALE)-1);
LPRINTF("update_drift: drift_comp %ld ", (long int)accum_drift);
fdrift = (fdrift * 1000) / (1<<USECSCALE);
fprintf(df, "%4d.%03d %c%ld.%06ld %.24s\n", idrift, fdrift,
(offset < 0) ? '-' : '+', (long int)(l_abs(offset) / 1000000),
(long int)(l_abs(offset) % 1000000), asctime(localtime(&reftime)));
fclose(df);
LPRINTF("update_drift: %d.%03d ppm ", idrift, fdrift);
}
}
static void
adjust_clock(
struct timeval *offset,
const char *drift_file,
time_t reftime
)
{
struct timeval toffset;
register long usecoffset;
int tmp;
if (no_set)
return;
if (skip_adjust)
{
skip_adjust = 0;
return;
}
toffset = *offset;
toffset.tv_sec = l_abs(toffset.tv_sec);
toffset.tv_usec = l_abs(toffset.tv_usec);
if (timercmp(&toffset, &max_adj_offset, >))
{
set_time(offset);
clock_adjust = 0;
skip_adjust = 1;
return;
}
usecoffset = offset->tv_sec * 1000000 + offset->tv_usec;
clock_adjust = R_SHIFT(usecoffset, TIMECONSTANT);
tmp = 0;
while (adjustments > (1 << tmp))
tmp++;
adjustments = 0;
if (tmp > FREQ_WEIGHT)
tmp = FREQ_WEIGHT;
accum_drift += R_SHIFT(usecoffset << USECSCALE, TIMECONSTANT+TIMECONSTANT+FREQ_WEIGHT-tmp);
if (accum_drift > MAX_DRIFT)
accum_drift = MAX_DRIFT;
else
if (accum_drift < -MAX_DRIFT)
accum_drift = -MAX_DRIFT;
update_drift(drift_file, usecoffset, reftime);
LPRINTF("clock_adjust: %s, clock_adjust %ld, drift_comp %ld(%ld) ",
pr_timeval(offset),(long int) R_SHIFT(clock_adjust, USECSCALE),
(long int)R_SHIFT(accum_drift, USECSCALE), (long int)accum_drift);
}
static void
periodic_adjust(
void
)
{
register long adjustment;
adjustments++;
adjustment = R_SHIFT(clock_adjust, PHASE_WEIGHT);
clock_adjust -= adjustment;
adjustment += R_SHIFT(accum_drift, USECSCALE+ADJINTERVAL);
adj_time(adjustment);
}
static void
tick(
int signum
)
{
static unsigned long last_notice = 0;
#if !defined(HAVE_SIGACTION) && !defined(HAVE_SIGVEC)
(void)signal(SIGALRM, tick);
#endif
periodic_adjust();
ticks += 1<<ADJINTERVAL;
if ((ticks - last_sync) > MAX_UNSYNC)
{
if (sync_state == SYNC)
{
sync_state = NO_SYNC;
syslog(LOG_INFO, "DCF77 reception lost (timeout)");
last_notice = ticks;
}
else
if ((ticks - last_notice) > NOTICE_INTERVAL)
{
syslog(LOG_NOTICE, "still not synchronized to DCF77 - check receiver/signal");
last_notice = ticks;
}
}
#ifndef ITIMER_REAL
(void) alarm(1<<ADJINTERVAL);
#endif
}
static void
detach(
void
)
{
# ifdef HAVE_DAEMON
daemon(0, 0);
# else
if (fork())
exit(0);
{
u_long s;
int max_fd;
#if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
max_fd = sysconf(_SC_OPEN_MAX);
#else
max_fd = getdtablesize();
#endif
for (s = 0; s < max_fd; s++)
(void) close((int)s);
(void) open("/", 0);
(void) dup2(0, 1);
(void) dup2(0, 2);
#ifdef SYS_DOMAINOS
{
uid_$t puid;
status_$t st;
proc2_$who_am_i(&puid);
proc2_$make_server(&puid, &st);
}
#endif
#if defined(HAVE_SETPGID) || defined(HAVE_SETSID)
# ifdef HAVE_SETSID
if (setsid() == (pid_t)-1)
syslog(LOG_ERR, "dcfd: setsid(): %m");
# else
if (setpgid(0, 0) == -1)
syslog(LOG_ERR, "dcfd: setpgid(): %m");
# endif
#else
{
int fid;
fid = open("/dev/tty", 2);
if (fid >= 0)
{
(void) ioctl(fid, (u_long) TIOCNOTTY, (char *) 0);
(void) close(fid);
}
# ifdef HAVE_SETPGRP_0
(void) setpgrp();
# else
(void) setpgrp(0, getpid());
# endif
}
#endif
}
#endif
}
static void
usage(
char *program
)
{
fprintf(stderr, "usage: %s [-n] [-f] [-l] [-t] [-i] [-o] [-d <drift_file>] [-D <input delay>] <device>\n", program);
fprintf(stderr, "\t-n do not change time\n");
fprintf(stderr, "\t-i interactive\n");
fprintf(stderr, "\t-t trace (print all datagrams)\n");
fprintf(stderr, "\t-f print all databits (includes PTB private data)\n");
fprintf(stderr, "\t-l print loop filter debug information\n");
fprintf(stderr, "\t-o print offet average for current minute\n");
fprintf(stderr, "\t-Y make internal Y2K checks then exit\n");
fprintf(stderr, "\t-d <drift_file> specify alternate drift file\n");
fprintf(stderr, "\t-D <input delay>specify delay from input edge to processing in micro seconds\n");
}
static int
check_y2k( void )
{
int year;
int year0 = 1900;
int yearend;
time_t Time;
struct tm LocalTime;
int Fatals, Warnings;
#define Error(year) if ( (year)>=2036 && LocalTime.tm_year < 110 ) \
Warnings++; else Fatals++
Fatals = Warnings = 0;
Time = time( (time_t *)NULL );
LocalTime = *localtime( &Time );
year = ( sizeof( u_long ) > 4 )
? ( 400 * 3 )
: ((int)(0x7FFFFFFF / 365.242 / 24/60/60)* 2 );
yearend = year0 + year;
year = 1900+YEAR_PIVOT;
printf( " starting year %04d\n", (int) year );
printf( " ending year %04d\n", (int) yearend );
for ( ; year < yearend; year++ )
{
clocktime_t ct;
time_t Observed;
time_t Expected;
unsigned Flag;
unsigned long t;
ct.day = 1;
ct.month = 1;
ct.year = year;
ct.hour = ct.minute = ct.second = ct.usecond = 0;
ct.utcoffset = 0;
ct.flags = 0;
Flag = 0;
Observed = dcf_to_unixtime( &ct, &Flag );
if ( ct.year != year )
{
fprintf( stdout,
"%04d: dcf_to_unixtime(,%d) CORRUPTED ct.year: was %d\n",
(int)year, (int)Flag, (int)ct.year );
Error(year);
break;
}
t = julian0(year) - julian0(1970);
Expected = t * 24 * 60 * 60;
if ( Observed != Expected || Flag )
{
fprintf( stdout,
"%04d: dcf_to_unixtime(,%d) FAILURE: was=%lu s/b=%lu (%ld)\n",
year, (int)Flag,
(unsigned long)Observed, (unsigned long)Expected,
((long)Observed - (long)Expected) );
Error(year);
break;
}
if ( year >= YEAR_PIVOT+1900 )
{
ct.year = year % 100;
Flag = 0;
Observed = dcf_to_unixtime( &ct, &Flag );
if ( Observed != Expected || Flag )
{
fprintf( stdout,
"%04d: dcf_to_unixtime(%d,%d) FAILURE: was=%lu s/b=%lu (%ld)\n",
year, (int)ct.year, (int)Flag,
(unsigned long)Observed, (unsigned long)Expected,
((long)Observed - (long)Expected) );
Error(year);
break;
}
ct.year = year - 1900;
Flag = 0;
Observed = dcf_to_unixtime( &ct, &Flag );
if ( Observed != Expected || Flag ) {
fprintf( stdout,
"%04d: dcf_to_unixtime(%d,%d) FAILURE: was=%lu s/b=%lu (%ld)\n",
year, (int)ct.year, (int)Flag,
(unsigned long)Observed, (unsigned long)Expected,
((long)Observed - (long)Expected) );
Error(year);
break;
}
}
}
return ( Fatals );
}
#if defined(TIOCMSET) && (defined(TIOCM_DTR) || defined(CIOCM_DTR))
static void
rawdcf_init(
int fd
)
{
#ifdef TIOCM_DTR
int sl232 = TIOCM_DTR;
#else
int sl232 = CIOCM_DTR;
#endif
if (ioctl(fd, TIOCMSET, (caddr_t)&sl232) == -1)
{
syslog(LOG_NOTICE, "rawdcf_init: WARNING: ioctl(fd, TIOCMSET, [C|T]IOCM_DTR): %m");
}
}
#else
static void
rawdcf_init(
int fd
)
{
syslog(LOG_NOTICE, "rawdcf_init: WARNING: OS interface incapable of setting DTR to power DCF modules");
}
#endif
int
main(
int argc,
char **argv
)
{
unsigned char c;
char **a = argv;
int ac = argc;
char *file = NULL;
const char *drift_file = "/etc/dcfd.drift";
int fd;
int offset = 15;
int offsets = 0;
int delay = DEFAULT_DELAY;
int trace = 0;
int errs = 0;
while (--ac)
{
char *arg = *++a;
if (*arg == '-')
while ((c = *++arg))
switch (c)
{
case 't':
trace = 1;
interactive = 1;
break;
case 'f':
offset = 0;
interactive = 1;
break;
case 'l':
loop_filter_debug = 1;
offsets = 1;
interactive = 1;
break;
case 'n':
no_set = 1;
break;
case 'o':
offsets = 1;
interactive = 1;
break;
case 'i':
interactive = 1;
break;
case 'D':
if (ac > 1)
{
delay = atoi(*++a);
ac--;
}
else
{
fprintf(stderr, "%s: -D requires integer argument\n", argv[0]);
errs=1;
}
break;
case 'd':
if (ac > 1)
{
drift_file = *++a;
ac--;
}
else
{
fprintf(stderr, "%s: -d requires file name argument\n", argv[0]);
errs=1;
}
break;
case 'Y':
errs=check_y2k();
exit( errs ? 1 : 0 );
default:
fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
errs=1;
break;
}
else
if (file == NULL)
file = arg;
else
{
fprintf(stderr, "%s: device specified twice\n", argv[0]);
errs=1;
}
}
if (errs)
{
usage(argv[0]);
exit(1);
}
else
if (file == NULL)
{
fprintf(stderr, "%s: device not specified\n", argv[0]);
usage(argv[0]);
exit(1);
}
errs = LINES+1;
fd = open(file, O_RDONLY);
if (fd == -1)
{
perror(file);
exit(1);
}
else
{
int i, rrc;
struct timeval t, tt, tlast;
struct timeval timeout;
struct timeval phase;
struct timeval time_offset;
char pbuf[61];
char buf[61];
clocktime_t clock_time;
time_t utc_time = 0;
time_t last_utc_time = 0;
long usecerror = 0;
long lasterror = 0;
#if defined(HAVE_TERMIOS_H) || defined(STREAM)
struct termios term;
#else
# if defined(HAVE_TERMIO_H) || defined(HAVE_SYSV_TTYS)
struct termio term;
# endif/* HAVE_TERMIO_H || HAVE_SYSV_TTYS */
#endif
unsigned int rtc = CVT_NONE;
rawdcf_init(fd);
timeout.tv_sec = 1;
timeout.tv_usec = 500000;
phase.tv_sec = 0;
phase.tv_usec = delay;
if (TTY_GETATTR(fd, &term) == -1)
{
perror("tcgetattr");
exit(1);
}
memset(term.c_cc, 0, sizeof(term.c_cc));
term.c_cc[VMIN] = 1;
#ifdef NO_PARENB_IGNPAR
term.c_cflag = CS8|CREAD|CLOCAL;
#else
term.c_cflag = CS8|CREAD|CLOCAL|PARENB;
#endif
term.c_iflag = IGNPAR;
term.c_oflag = 0;
term.c_lflag = 0;
cfsetispeed(&term, B50);
cfsetospeed(&term, B50);
if (TTY_SETATTR(fd, &term) == -1)
{
perror("tcsetattr");
exit(1);
}
if (!interactive)
detach();
#ifdef LOG_DAEMON
openlog("dcfd", LOG_PID, LOG_DAEMON);
#else
openlog("dcfd", LOG_PID);
#endif
#ifdef HAVE_SIGACTION
{
struct sigaction act;
act.sa_handler = tick;
# ifdef HAVE_SA_SIGACTION_IN_STRUCT_SIGACTION
act.sa_sigaction = (void (*) P((int, siginfo_t *, void *)))0;
# endif
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGALRM, &act, (struct sigaction *)0) == -1)
{
syslog(LOG_ERR, "sigaction(SIGALRM): %m");
exit(1);
}
}
#else
#ifdef HAVE_SIGVEC
{
struct sigvec vec;
vec.sv_handler = tick;
vec.sv_mask = 0;
vec.sv_flags = 0;
if (sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1)
{
syslog(LOG_ERR, "sigvec(SIGALRM): %m");
exit(1);
}
}
#else
(void) signal(SIGALRM, tick);
#endif
#endif
#ifdef ITIMER_REAL
{
struct itimerval it;
it.it_interval.tv_sec = 1<<ADJINTERVAL;
it.it_interval.tv_usec = 0;
it.it_value.tv_sec = 1<<ADJINTERVAL;
it.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL, &it, (struct itimerval *)0) == -1)
{
syslog(LOG_ERR, "setitimer: %m");
exit(1);
}
}
#else
(void) alarm(1<<ADJINTERVAL);
#endif
PRINTF(" DCF77 monitor %s - Copyright (C) 1993-2005 by Frank Kardel\n\n", revision);
pbuf[60] = '\0';
for ( i = 0; i < 60; i++)
pbuf[i] = '.';
read_drift(drift_file);
gettimeofday(&tlast, 0L);
i = 0;
do
{
while ((rrc = read(fd, &c, 1)) == 1)
{
gettimeofday(&t, 0L);
tt = t;
timersub(&t, &tlast);
if (errs > LINES)
{
PRINTF(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
PRINTF(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
errs = 0;
}
if (timercmp(&t, &timeout, >))
{
PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]);
if ((rtc = cvt_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
{
PRINTF("\n");
if (sync_state == SYNC)
{
sync_state = NO_SYNC;
syslog(LOG_INFO, "DCF77 reception lost (bad data)");
}
errs++;
}
else
if (trace)
{
PRINTF("\r %.*s ", 59 - offset, &buf[offset]);
}
buf[0] = c;
if (((c^0xFF)+1) & (c^0xFF))
pbuf[0] = '?';
else
pbuf[0] = type(c) ? '#' : '-';
for ( i = 1; i < 60; i++)
pbuf[i] = '.';
i = 0;
}
else
{
buf[i] = c;
if (((c^0xFF)+1) & (c^0xFF))
pbuf[i] = '?';
else
pbuf[i] = type(c) ? '#' : '-';
PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]);
}
if (i == 0 && rtc == CVT_OK)
{
if ((utc_time = dcf_to_unixtime(&clock_time, &rtc)) == -1)
{
PRINTF("*** BAD CONVERSION\n");
}
if (utc_time != (last_utc_time + 60))
{
PRINTF("*** NO MINUTE INC\n");
if (sync_state == SYNC)
{
sync_state = NO_SYNC;
syslog(LOG_INFO, "DCF77 reception lost (data mismatch)");
}
errs++;
rtc = CVT_FAIL|CVT_BADTIME|CVT_BADDATE;
}
else
usecerror = 0;
last_utc_time = utc_time;
}
if (rtc == CVT_OK)
{
if (i == 0)
{
last_sync = ticks;
if (sync_state == NO_SYNC)
{
syslog(LOG_INFO, "receiving DCF77");
}
else
{
time_offset.tv_sec = lasterror / 1000000;
time_offset.tv_usec = lasterror % 1000000;
adjust_clock(&time_offset, drift_file, utc_time);
}
sync_state = SYNC;
}
time_offset.tv_sec = utc_time + i;
time_offset.tv_usec = 0;
timeradd(&time_offset, &phase);
usecerror += (time_offset.tv_sec - tt.tv_sec) * 1000000 + time_offset.tv_usec
-tt.tv_usec;
PRINTF(offsets ? "%s, %2ld:%02ld:%02d, %ld.%02ld.%02ld, <%s%s%s%s> (%c%ld.%06lds)" :
"%s, %2ld:%02ld:%02d, %ld.%02ld.%02ld, <%s%s%s%s>",
wday[clock_time.wday],
clock_time.hour, clock_time.minute, i, clock_time.day, clock_time.month,
clock_time.year,
(clock_time.flags & DCFB_ALTERNATE) ? "R" : "_",
(clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
(clock_time.flags & DCFB_DST) ? "D" : "_",
(clock_time.flags & DCFB_LEAP) ? "L" : "_",
(lasterror < 0) ? '-' : '+', l_abs(lasterror) / 1000000, l_abs(lasterror) % 1000000
);
if (trace && (i == 0))
{
PRINTF("\n");
errs++;
}
lasterror = usecerror / (i+1);
}
else
{
lasterror = 0;
}
PRINTF("\r");
if (i < 60)
{
i++;
}
tlast = tt;
if (interactive)
fflush(stdout);
}
} while ((rrc == -1) && (errno == EINTR));
syslog(LOG_ERR, "TERMINATING - cannot read from device %s (%m)", file);
(void)close(fd);
}
closelog();
return 0;
}