123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332 |
- #include "gpsd_config.h"
- #include <errno.h>
- #include <inttypes.h>
- #include <limits.h>
- #include <math.h>
- #include <pthread.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <unistd.h>
- #define FAKE_PPS_NAME "ktimer"
- #if defined(HAVE_SYS_TIMEPPS_H)
- #include <unistd.h>
- #include <sys/timepps.h>
- #endif
- #include "timespec.h"
- #include "ppsthread.h"
- #include "os_compat.h"
- #ifndef _REENTRANT
- #define _REENTRANT
- #endif
- #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
- #define _POSIX_THREAD_SAFE_FUNCTIONS
- #endif
- #include <sys/ioctl.h>
- #if defined(HAVE_SYS_TIMEPPS_H)
- #include <glob.h>
- #include <fcntl.h>
- #endif
- #if defined(TIOCMIWAIT)
- static int get_edge_tiocmiwait( volatile struct pps_thread_t *,
- struct timespec *, int *,
- volatile struct timedelta_t *);
- #endif
- struct inner_context_t {
- volatile struct pps_thread_t *pps_thread;
- bool pps_canwait;
- #if defined(HAVE_SYS_TIMEPPS_H)
- int pps_caps;
- pps_handle_t kernelpps_handle;
- #endif
- };
- #if defined(HAVE_SYS_TIMEPPS_H)
- static int get_edge_rfc2783(struct inner_context_t *,
- struct timespec *,
- int *,
- struct timespec *,
- int *,
- volatile struct timedelta_t *);
- #endif
- static pthread_mutex_t ppslast_mutex = PTHREAD_MUTEX_INITIALIZER;
- static void pps_strerror_r(int errnum, char *buf, size_t len)
- {
- if (strerror_r(errnum, buf, len)) {
- return;
- } else {
- return;
- }
- }
- static void thread_lock(volatile struct pps_thread_t *pps_thread)
- {
- int pthread_err = pthread_mutex_lock(&ppslast_mutex);
- if ( 0 != pthread_err ) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_ERROR,
- "PPS:%s pthread_mutex_lock() : %s\n",
- pps_thread->devicename, errbuf);
- }
- }
- static void thread_unlock(volatile struct pps_thread_t *pps_thread)
- {
- int pthread_err = pthread_mutex_unlock(&ppslast_mutex);
- if ( 0 != pthread_err ) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_ERROR,
- "TPPS:%s pthread_mutex_unlock() : %s\n",
- pps_thread->devicename, errno, errbuf);
- }
- }
- #if defined(HAVE_SYS_TIMEPPS_H)
- #ifdef __linux__
- static void get_sysfs_var(const char *path, char *buf, size_t bufsize)
- {
- buf[0] = '\0';
- int fd = open(path, O_RDONLY);
- if ( 0 <= fd ) {
- ssize_t r = read( fd, buf, bufsize -1);
- if ( 0 < r ) {
- buf[r - 1] = '\0';
- }
- (void)close(fd);
- }
- }
- int pps_check_fake(const char *name) {
- char path[PATH_MAX] = "";
- char buf[32] = "";
- snprintf(path, sizeof(path), "/sys/devices/virtual/pps/%s/name", name);
- get_sysfs_var(path, buf, sizeof(buf));
- return strcmp(buf, FAKE_PPS_NAME) == 0;
- }
- char *pps_get_first(void)
- {
- if (pps_check_fake("pps0"))
- return "/dev/pps1";
- return "/dev/pps0";
- }
- #endif
- static int init_kernel_pps(struct inner_context_t *inner_context)
- {
- pps_params_t pp;
- int ret;
- #ifdef __linux__
-
- int ldisc = 18;
- glob_t globbuf;
- #endif
- char path[PATH_MAX] = "";
- volatile struct pps_thread_t *pps_thread = inner_context->pps_thread;
- inner_context->kernelpps_handle = -1;
- inner_context->pps_canwait = false;
-
- #ifndef __clang_analyzer__
- ret = -1;
- #endif
- #ifdef __linux__
-
- if (strncmp(pps_thread->devicename, "/dev/pps", 8) == 0) {
- if (pps_check_fake(pps_thread->devicename + 5))
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s is fake PPS,"
- " timing will be inaccurate\n",
- pps_thread->devicename);
- (void)strncpy(path, pps_thread->devicename, sizeof(path)-1);
- }
- else {
- char pps_num = '\0';
- size_t i;
-
-
-
-
- if ( 0 > ioctl(pps_thread->devicefd, TIOCSETD, &ldisc)) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s cannot set PPS line discipline %s\n",
- pps_thread->devicename, errbuf);
- return -1;
- }
-
-
- memset( (void *)&globbuf, 0, sizeof(globbuf));
- (void)glob("/sys/devices/virtual/pps/pps?/path", 0, NULL, &globbuf);
- memset( (void *)&path, 0, sizeof(path));
- for ( i = 0; i < globbuf.gl_pathc; i++ ) {
- get_sysfs_var(globbuf.gl_pathv[i], path, sizeof(path));
- pps_thread->log_hook(pps_thread, THREAD_PROG,
- "KPPS:%s checking %s, %s\n",
- pps_thread->devicename,
- globbuf.gl_pathv[i], path);
- if ( 0 == strncmp( path, pps_thread->devicename, sizeof(path))) {
-
-
- pps_num = globbuf.gl_pathv[i][28];
- break;
- }
- memset( (void *)&path, 0, sizeof(path));
- }
-
- globfree(&globbuf);
- if ( 0 == (int)pps_num ) {
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s device not found.\n",
- pps_thread->devicename);
- return -1;
- }
-
- (void)snprintf(path, sizeof(path), "/dev/pps%c", pps_num);
- }
-
- ret = open(path, O_RDWR);
- if ( 0 > ret ) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s running as %d/%d, cannot open %s: %s\n",
- pps_thread->devicename,
- getuid(), geteuid(),
- path, errbuf);
- return -1;
- }
- #else
-
- (void)strlcpy(path, pps_thread->devicename, sizeof(path));
-
- ret = pps_thread->devicefd;
- #endif
-
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s RFC2783 path:%s, fd is %d\n",
- pps_thread->devicename, path,
- ret);
-
- if ( 0 > time_pps_create(ret, (pps_handle_t *)&inner_context->kernelpps_handle )) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s time_pps_create(%d) failed: %s\n",
- pps_thread->devicename,
- ret, errbuf);
- return -1;
- }
-
-
- inner_context->pps_caps = 0;
- if ( 0 > time_pps_getcap(inner_context->kernelpps_handle,
- &inner_context->pps_caps)) {
- char errbuf[BUFSIZ] = "unknown error";
- inner_context->pps_caps = 0;
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s time_pps_getcap() failed: %.100s\n",
- pps_thread->devicename, errbuf);
- return -1;
- } else {
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s pps_caps 0x%02X\n",
- pps_thread->devicename,
- inner_context->pps_caps);
- }
-
- memset( (void *)&pp, 0, sizeof(pps_params_t));
- pp.api_version = PPS_API_VERS_1;
- if ( 0 == (PPS_TSFMT_TSPEC & inner_context->pps_caps ) ) {
-
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s fail, missing mandatory PPS_TSFMT_TSPEC\n",
- pps_thread->devicename);
- return -1;
- }
- if ( 0 != (PPS_CANWAIT & inner_context->pps_caps ) ) {
-
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s have PPS_CANWAIT\n",
- pps_thread->devicename);
- inner_context->pps_canwait = true;
- }
- pp.mode = PPS_TSFMT_TSPEC;
- switch ( (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR) & inner_context->pps_caps ) {
- case PPS_CAPTUREASSERT:
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s missing PPS_CAPTURECLEAR, pulse may be offset\n",
- pps_thread->devicename);
- pp.mode |= PPS_CAPTUREASSERT;
- break;
- case PPS_CAPTURECLEAR:
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s missing PPS_CAPTUREASSERT, pulse may be offset\n",
- pps_thread->devicename);
- pp.mode |= PPS_CAPTURECLEAR;
- break;
- case PPS_CAPTUREASSERT | PPS_CAPTURECLEAR:
- pp.mode |= PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
- break;
- default:
-
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s missing PPS_CAPTUREASSERT and CLEAR\n",
- pps_thread->devicename);
- return -1;
- }
- if ( 0 > time_pps_setparams(inner_context->kernelpps_handle, &pp)) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- pps_thread->log_hook(pps_thread, THREAD_ERROR,
- "KPPS:%s time_pps_setparams(mode=0x%02X) failed: %s\n",
- pps_thread->devicename, pp.mode,
- errbuf);
- (void)time_pps_destroy(inner_context->kernelpps_handle);
- return -1;
- }
- return 0;
- }
- #endif
- #if defined(TIOCMIWAIT)
- static int get_edge_tiocmiwait( volatile struct pps_thread_t *thread_context,
- struct timespec *clock_ts,
- int *state,
- volatile struct timedelta_t *last_fixtime)
- {
- char ts_str[TIMESPEC_LEN];
-
- #define PPS_LINE_TIOC (TIOCM_CD|TIOCM_RI|TIOCM_CTS|TIOCM_DSR)
-
- if (ioctl(thread_context->devicefd, TIOCMIWAIT, PPS_LINE_TIOC) != 0) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- thread_context->log_hook(thread_context, THREAD_WARN,
- "TPPS:%s ioctl(TIOCMIWAIT) failed: %d %.40s\n",
- thread_context->devicename, errno, errbuf);
- return -1;;
- }
-
-
-
- thread_lock(thread_context);
- *last_fixtime = thread_context->fix_in;
- thread_unlock(thread_context);
-
-
- if ( 0 > clock_gettime(CLOCK_REALTIME, clock_ts) ) {
-
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- thread_context->log_hook(thread_context, THREAD_ERROR,
- "TPPS:%s clock_gettime() failed: %.100s\n",
- thread_context->devicename, errbuf);
- return -1;;
- }
-
- if (ioctl(thread_context->devicefd, (unsigned long)TIOCMGET, state) != 0) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- thread_context->log_hook(thread_context, THREAD_ERROR,
- "TPPS:%s ioctl(TIOCMGET) failed: %.100s\n",
- thread_context->devicename, errbuf);
- return -1;
- }
-
-
- *state &= PPS_LINE_TIOC;
- thread_context->log_hook(thread_context, THREAD_PROG,
- "TPPS:%s ioctl(TIOCMIWAIT) succeeded, time:%s, state: %d\n",
- thread_context->devicename,
- timespec_str(clock_ts, ts_str, sizeof(ts_str)),
- *state);
- return 0;
- }
- #endif
- #if defined(HAVE_SYS_TIMEPPS_H)
- static int get_edge_rfc2783(struct inner_context_t *inner_context,
- struct timespec *prev_clock_ts,
- int *prev_edge,
- struct timespec *clock_ts,
- int *edge,
- volatile struct timedelta_t *last_fixtime)
- {
- pps_info_t pi;
- char ts_str1[TIMESPEC_LEN], ts_str2[TIMESPEC_LEN];
- struct timespec kernelpps_tv;
- volatile struct pps_thread_t *thread_context = inner_context->pps_thread;
- if ( inner_context->pps_canwait ) {
-
- kernelpps_tv.tv_sec = 3;
- kernelpps_tv.tv_nsec = 0;
- } else {
-
- memset( (void *)&kernelpps_tv, 0, sizeof(kernelpps_tv));
- }
- memset( (void *)&pi, 0, sizeof(pi));
- if ( 0 > time_pps_fetch(inner_context->kernelpps_handle, PPS_TSFMT_TSPEC
- , &pi, &kernelpps_tv)) {
- char errbuf[BUFSIZ] = "unknown error";
- pps_strerror_r(errno, errbuf, sizeof(errbuf));
- if ( ETIMEDOUT == errno || EINTR == errno ) {
-
- thread_context->log_hook(thread_context, THREAD_INF,
- "KPPS:%s kernel PPS timeout %s\n",
- thread_context->devicename, errbuf);
- return 1;
- }
- thread_context->log_hook(thread_context, THREAD_WARN,
- "KPPS:%s kernel PPS failed %s\n",
- thread_context->devicename, errbuf);
- return 0;
- }
- if ( inner_context->pps_canwait ) {
-
-
- thread_lock(thread_context);
- *last_fixtime = thread_context->fix_in;
- thread_unlock(thread_context);
- }
-
- if ( pi.assert_timestamp.tv_sec > pi.clear_timestamp.tv_sec ) {
-
- *edge = 1;
- } else if ( pi.assert_timestamp.tv_sec < pi.clear_timestamp.tv_sec ) {
-
- *edge = 0;
- } else if ( pi.assert_timestamp.tv_nsec > pi.clear_timestamp.tv_nsec ) {
-
- *edge = 1;
- } else {
-
- *edge = 0;
- }
- if ( 1 == *edge ) {
-
- *prev_edge = 0;
- if ( 0 == pi.clear_timestamp.tv_sec ) {
-
- *prev_clock_ts = pi.assert_timestamp;
- } else {
- *prev_clock_ts = pi.clear_timestamp;
- }
- *clock_ts = pi.assert_timestamp;
- } else {
-
- *prev_edge = 1;
- *prev_clock_ts = pi.assert_timestamp;
- *clock_ts = pi.clear_timestamp;
- }
-
- thread_context->log_hook(thread_context, THREAD_PROG,
- "KPPS:%s assert %s, sequence: %lu, "
- "clear %s, sequence: %lu - using: %.10s\n",
- thread_context->devicename,
- timespec_str(&pi.assert_timestamp, ts_str1, sizeof(ts_str1)),
- (unsigned long) pi.assert_sequence,
- timespec_str(&pi.clear_timestamp, ts_str2, sizeof(ts_str2)),
- (unsigned long) pi.clear_sequence,
- *edge ? "assert" : "clear");
- return 0;
- }
- #endif
- static void *gpsd_ppsmonitor(void *arg)
- {
- char ts_str1[TIMESPEC_LEN], ts_str2[TIMESPEC_LEN];
- struct inner_context_t inner_context = *((struct inner_context_t *)arg);
- volatile struct pps_thread_t *thread_context = inner_context.pps_thread;
-
- volatile struct timedelta_t last_fixtime = {{0, 0}, {0, 0}};
- struct timespec clock_ts = {0, 0};
- time_t last_second_used = 0;
- int64_t cycle = 0, duration = 0;
-
- int state = 0;
-
- int unchanged = 0;
-
- int state_last = 0;
-
- int edge = 0;
- #if defined(TIOCMIWAIT)
- int edge_tio = 0;
- long long cycle_tio = 0;
- int64_t duration_tio = 0;
- int state_tio = 0;
- int state_last_tio = 0;
- struct timespec clock_ts_tio = {0, 0};
-
- struct timespec pulse_tio[2] = { {0, 0}, {0, 0} };
- #endif
- #if defined(HAVE_SYS_TIMEPPS_H)
- int64_t cycle_kpps = 0, duration_kpps = 0;
-
- struct timespec pulse_kpps[2] = { {0, 0}, {0, 0} };
- #endif
- bool not_a_tty = false;
-
- ((volatile struct inner_context_t *)arg)->pps_thread = NULL;
-
- if ( 0 > thread_context->devicefd
- || 0 == isatty(thread_context->devicefd) ) {
- thread_context->log_hook(thread_context, THREAD_PROG,
- "KPPS:%s gps_fd:%d not a tty, can not use TIOMCIWAIT\n",
- thread_context->devicename,
- thread_context->devicefd);
-
- not_a_tty = true;
- }
-
- if ( not_a_tty && !inner_context.pps_canwait ) {
-
- }
-
- while (thread_context->report_hook != NULL) {
- bool ok = false;
- char *log = NULL;
- char *edge_str = "";
- if (++unchanged == 10) {
-
- unchanged = 0;
- thread_context->log_hook(thread_context, THREAD_WARN,
- "PPS:%s unchanged state, ppsmonitor sleeps 10\n",
- thread_context->devicename);
- (void)sleep(10);
- }
-
- #if defined(TIOCMIWAIT)
- if ( !not_a_tty && !inner_context.pps_canwait ) {
- int ret;
-
-
- ret = get_edge_tiocmiwait( thread_context, &clock_ts_tio,
- &state_tio, &last_fixtime );
- if ( 0 != ret ) {
- thread_context->log_hook(thread_context, THREAD_PROG,
- "PPS:%s die: TIOCMIWAIT Error\n",
- thread_context->devicename);
- break;
- }
- edge_tio = (state_tio > state_last_tio) ? 1 : 0;
- state_last_tio = state_tio;
-
-
- cycle_tio = timespec_diff_ns(clock_ts_tio, pulse_tio[edge_tio]);
- cycle_tio /= 1000;
- duration_tio = timespec_diff_ns(clock_ts_tio,
- pulse_tio[edge_tio ? 0 : 1])/1000;
-
- pulse_tio[edge_tio] = clock_ts_tio;
-
- ok = true;
- clock_ts = clock_ts_tio;
- state = edge_tio;
- edge = edge_tio;
- edge_str = edge ? "Assert" : "Clear";
- cycle = cycle_tio;
- duration = duration_tio;
- thread_context->log_hook(thread_context, THREAD_PROG,
- "TPPS:%s %.10s, cycle: " PRId64 ", duration: " PRId64
- " @ %s\n",
- thread_context->devicename, edge_str, cycle, duration,
- timespec_str(&clock_ts, ts_str1, sizeof(ts_str1)));
- }
- #endif
-
- log = NULL;
- #if defined(HAVE_SYS_TIMEPPS_H)
- if ( 0 <= inner_context.kernelpps_handle ) {
- int ret;
- int edge_kpps = 0;
-
- struct timespec clock_ts_kpps = {0, 0};
-
- struct timespec prev_clock_ts = {0, 0};
-
- int prev_edge = 0;
-
- ret = get_edge_rfc2783(&inner_context,
- &prev_clock_ts,
- &prev_edge,
- &clock_ts_kpps,
- &edge_kpps,
- &last_fixtime);
- if ( -1 == ret ) {
-
- thread_context->log_hook(thread_context, THREAD_ERROR,
- "PPS:%s die: RFC2783 Error\n",
- thread_context->devicename);
- break;
- }
- if ( 1 == ret ) {
-
-
- continue;
- }
-
-
- cycle_kpps = timespec_diff_ns(clock_ts_kpps, pulse_kpps[edge_kpps]);
- cycle_kpps /= 1000;
-
- duration_kpps = timespec_diff_ns(clock_ts_kpps, prev_clock_ts) /
- 1000;
-
- pulse_kpps[edge_kpps] = clock_ts_kpps;
- pulse_kpps[edge_kpps ? 0 : 1] = prev_clock_ts;
-
-
- state = edge_kpps;
- edge = edge_kpps;
- edge_str = edge ? "Assert" : "Clear";
- clock_ts = clock_ts_kpps;
- cycle = cycle_kpps;
- duration = duration_kpps;
- thread_context->log_hook(thread_context, THREAD_PROG,
- "KPPS:%s %.10s cycle: " PRId64 ", duration: " PRId64 " @ %s\n",
- thread_context->devicename,
- edge_str,
- cycle_kpps, duration_kpps,
- timespec_str(&clock_ts_kpps, ts_str1, sizeof(ts_str1)));
- }
- #endif
- if ( not_a_tty && !inner_context.pps_canwait ) {
-
- thread_context->log_hook(thread_context, THREAD_WARN,
- "PPS:%s die: no TIOMCIWAIT, nor RFC2783 CANWAIT\n",
- thread_context->devicename);
- break;
- }
-
-
-
- if (state != state_last) {
- thread_context->log_hook(thread_context, THREAD_RAW,
- "PPS:%s %.10s pps-detect changed to %d\n",
- thread_context->devicename, edge_str, state);
- unchanged = 0;
- } else if ( (180000 < cycle && 220000 > cycle)
- || (900000 < cycle && 1100000 > cycle)
- || (1800000 < cycle && 2200000 > cycle) ) {
-
- duration = 0;
- unchanged = 0;
- thread_context->log_hook(thread_context, THREAD_RAW,
- "PPS:%s %.10s pps-detect invisible pulse\n",
- thread_context->devicename, edge_str);
- }
-
- state_last = state;
- thread_context->log_hook(thread_context, THREAD_PROG,
- "PPS:%s %.10s cycle: " PRId64 ", duration: " PRId64 " @ %s\n",
- thread_context->devicename,
- edge_str,
- cycle, duration,
- timespec_str(&clock_ts, ts_str1, sizeof(ts_str1)));
- if (unchanged) {
-
- continue;
- }
-
- log = "Unknown error";
- if ( 0 > cycle ) {
- log = "Rejecting negative cycle\n";
- } else if (180000 > cycle) {
-
- log = "Too short for 5Hz\n";
- } else if (201000 > cycle) {
-
-
- if (100000 > duration) {
-
-
- ok = true;
- log = "5Hz PPS pulse\n";
- }
- } else if (900000 > cycle) {
-
-
- log = "Too long for 5Hz, too short for 1Hz\n";
- } else if (1100000 > cycle) {
-
-
-
- if (0 == duration) {
- ok = true;
- log = "invisible pulse\n";
- } else if (450000 > duration) {
-
- log = "1Hz trailing edge\n";
- } else if (555000 > duration) {
-
-
- if (edge == 1) {
- ok = true;
- log = "square\n";
- }
- } else {
-
- ok = true;
- log = "1Hz leading edge\n";
- }
- } else if (1800000 > cycle) {
-
- log = "Too long for 1Hz, too short for 2Hz\n";
- } else if (2200000 > cycle) {
-
- if (990000 > duration) {
-
- log = "0.5 Hz square too short duration\n";
- } else if (1100000 > duration) {
-
- ok = true;
- log = "0.5 Hz square wave\n";
- } else {
- log = "0.5 Hz square too long duration\n";
- }
- } else {
-
- log = "Too long for 0.5Hz\n";
- }
-
-
-
-
- if (last_fixtime.real.tv_sec == 0) {
-
- ok = false;
- log = "missing last_fixtime\n";
- } else if ( ok && last_second_used >= last_fixtime.real.tv_sec ) {
-
- ok = false;
- log = "this second already handled\n";
- }
- if ( !ok ) {
-
- thread_context->log_hook(thread_context, THREAD_PROG,
- "PPS:%s %.10s ignored %.100s",
- thread_context->devicename, edge_str, log);
- continue;
- }
-
- unchanged = 0;
-
- struct timespec offset;
-
- char offset_str[TIMESPEC_LEN];
-
- struct timespec delay;
-
- char delay_str[TIMESPEC_LEN];
- char *log1 = "";
-
- struct timedelta_t ppstimes;
- thread_context->log_hook(thread_context, THREAD_RAW,
- "PPS:%s %.10s categorized %.100s",
- thread_context->devicename, edge_str, log);
-
-
- ppstimes.real.tv_sec = (time_t)last_fixtime.real.tv_sec + 1;
- ppstimes.real.tv_nsec = 0;
- ppstimes.clock = clock_ts;
-
- TS_SUB( &offset, &ppstimes.real, &ppstimes.clock);
- TS_SUB( &delay, &ppstimes.clock, &last_fixtime.clock);
- timespec_str(&delay, delay_str, sizeof(delay_str));
-
-
- if (0 > delay.tv_sec || 0 > delay.tv_nsec) {
- thread_context->log_hook(thread_context, THREAD_RAW,
- "PPS:%s %.10s system clock went backwards: %.20s\n",
- thread_context->devicename,
- edge_str,
- delay_str);
- log1 = "system clock went backwards";
- } else if ((2 < delay.tv_sec)
- || (1 == delay.tv_sec && 100000000 < delay.tv_nsec)) {
-
-
- thread_context->log_hook(thread_context, THREAD_RAW,
- "PPS:%s %.10s no current GPS seconds: %.20s\n",
- thread_context->devicename,
- edge_str,
- delay_str);
- log1 = "timestamp out of range";
- } else {
- last_second_used = last_fixtime.real.tv_sec;
- if (thread_context->report_hook != NULL)
- log1 = thread_context->report_hook(thread_context, &ppstimes);
- else
- log1 = "no report hook";
- thread_lock(thread_context);
- thread_context->pps_out = ppstimes;
- thread_context->ppsout_count++;
- thread_unlock(thread_context);
- thread_context->log_hook(thread_context, THREAD_INF,
- "PPS:%s %.10s hooks called clock: %s real: %s: %.20s\n",
- thread_context->devicename,
- edge_str,
- timespec_str(&ppstimes.clock, ts_str1, sizeof(ts_str1)),
- timespec_str(&ppstimes.real, ts_str2, sizeof(ts_str2)),
- log1);
- }
- thread_context->log_hook(thread_context, THREAD_PROG,
- "PPS:%s %.10s %.30s @ %s offset %.20s\n",
- thread_context->devicename,
- edge_str,
- log1,
- timespec_str(&clock_ts, ts_str1, sizeof(ts_str1)),
- timespec_str(&offset, offset_str, sizeof(offset_str)));
-
- }
- #if defined(HAVE_SYS_TIMEPPS_H)
- if (inner_context.kernelpps_handle > 0) {
- thread_context->log_hook(thread_context, THREAD_PROG,
- "KPPS:%s descriptor cleaned up\n",
- thread_context->devicename);
- (void)time_pps_destroy(inner_context.kernelpps_handle);
- }
- #endif
- thread_context->log_hook(thread_context, THREAD_PROG,
- "PPS:%s gpsd_ppsmonitor exited.\n",
- thread_context->devicename);
- return NULL;
- }
- void pps_thread_activate(volatile struct pps_thread_t *pps_thread)
- {
- int retval;
- pthread_t pt;
- struct timespec start_delay = {0, 1000000};
-
- static struct inner_context_t inner_context;
- inner_context.pps_thread = pps_thread;
- #if defined(HAVE_SYS_TIMEPPS_H)
-
- (void)init_kernel_pps(&inner_context);
- if ( 0 <= inner_context.kernelpps_handle ) {
- pps_thread->log_hook(pps_thread, THREAD_INF,
- "KPPS:%s kernel PPS will be used\n",
- pps_thread->devicename);
- } else {
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s kernel PPS unavailable, PPS accuracy will suffer\n",
- pps_thread->devicename);
- }
- #else
- pps_thread->log_hook(pps_thread, THREAD_WARN,
- "KPPS:%s no HAVE_SYS_TIMEPPS_H, PPS accuracy will suffer\n",
- pps_thread->devicename);
- #endif
- memset( &pt, 0, sizeof(pt));
- retval = pthread_create(&pt, NULL, gpsd_ppsmonitor, (void *)&inner_context);
- pps_thread->log_hook(pps_thread, THREAD_PROG, "PPS:%s thread %s\n",
- pps_thread->devicename,
- (retval==0) ? "launched" : "FAILED");
-
- while (inner_context.pps_thread)
- (void) nanosleep(&start_delay, NULL);
- }
- void pps_thread_deactivate(volatile struct pps_thread_t *pps_thread)
- {
- pps_thread->report_hook = NULL;
- }
- void pps_thread_fixin(volatile struct pps_thread_t *pps_thread,
- volatile struct timedelta_t *fix_in)
- {
- thread_lock(pps_thread);
- pps_thread->fix_in = *fix_in;
- thread_unlock(pps_thread);
- }
- void pps_thread_qErrin(volatile struct pps_thread_t *pps_thread,
- long qErr, struct timespec qErr_time)
- {
- thread_lock(pps_thread);
- pps_thread->qErr = qErr;
- pps_thread->qErr_time = qErr_time;
- thread_unlock(pps_thread);
- }
- int pps_thread_ppsout(volatile struct pps_thread_t *pps_thread,
- volatile struct timedelta_t *td)
- {
- volatile int ret;
- thread_lock(pps_thread);
- *td = pps_thread->pps_out;
- ret = pps_thread->ppsout_count;
- thread_unlock(pps_thread);
- return ret;
- }
|