wr_date.c 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Trivial tool to set WR date in the switch from local or NTP time
 *
 * Alessandro Rubini, 2011, for CERN, 2013. GPL2 or later
 */
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
19
#include <sys/timex.h>
20
#include "../../kernel/wbgen-regs/ppsg-regs.h"
21
#include <libwr/softpll_export.h>
22
#include <libwr/util.h>
23
#include <time_lib.h>
24
#include <rt_ipc.h>
25

26 27 28
#ifndef MOD_TAI
#define MOD_TAI 0x80
#endif
29
#define WRDATE_CFG_FILE "/etc/wr_date.conf"
30 31 32

/* Address for hardware, from nic-hardware.h */
#define FPGA_BASE_PPSG  0x10010500
33
#define PPSG_STEP_IN_NS 16 /* we count a 16.5MHz */
34

35 36 37
extern int init_module(void *module, unsigned long len, const char *options);
extern int delete_module(const char *module, unsigned int flags);

Jean-Claude BAU's avatar
Jean-Claude BAU committed
38 39 40 41 42
static int opt_verbose, opt_force, opt_not;
static char *opt_cfgfile = WRDATE_CFG_FILE;
static char *prgname;

void help(void)
43 44 45 46
{
	fprintf(stderr, "%s: Use: \"%s [<options>] <cmd> [<args>]\n",
		prgname, prgname);
	fprintf(stderr,
47
		"  The program uses %s as default config file name\n"
48 49 50
		"  -f       force: run even if not on a WR switch\n"
		"  -c <cfg> configfile to use in place of the default\n"
		"  -v       verbose: report what the program does\n"
Jean-Claude BAU's avatar
Jean-Claude BAU committed
51
		"  -n       do not act in practice - dry run\n"
52
		"    get             print WR time to stdout\n"
53
		"    get tohost      print WR time and set system time\n"
54
		"    set <value>     set WR time to scalar seconds\n"
Jean-Claude BAU's avatar
Jean-Claude BAU committed
55 56
		"    set host [tai]  set TAI and WR time from current host time.\n"
		"                    if tai option is set then set only the TAI offset.\n"
57
		"    stat            print statistics between TAI (WR time) and linux UTC\n"
58
		"    diff            show the difference between WR FPGA time (HW) and linux time (SW)\n"
59 60 61
/*		"    set ntp         set TAI from ntp and leap seconds" */
/*		"    set ntp:<ip>    set from specified ntp server\n" */
		, WRDATE_CFG_FILE);
62 63 64 65 66
	exit(1);
}


/* Check that we actualy are on the wr switch, exit if not */
67
int wrdate_check_host(void)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
	int ret;

	/*
	 * Check the specific CPU: a different switch will require a
	 * different memory address so this must be match.
	 * system(3) is bad, but it's fast to code
	 */
	ret = system("grep -q ARM926EJ-S /proc/cpuinfo");
	if (opt_force && ret) {
		fprintf(stderr, "%s: not running on the WR switch\n", prgname);
		if (!opt_not)
			exit(1);
	}
	return ret ? -1 : 0;
}

int wrdate_cfgfile(char *fname)
{
	/* FIXME: parse config file */
	return 0;
}

91

92
/* This returns wr time, used for syncing to a second transition */
93
uint64_t gettimeof_wr(struct timeval *tv, volatile struct PPSG_WB *pps)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
	uint32_t tai_h,tai_l,nsec, tmp1, tmp2;
	uint64_t tai;

	tai_h = pps->CNTR_UTCHI;

	do {
		tai_l = pps->CNTR_UTCLO;
		nsec = pps->CNTR_NSEC * PPSG_STEP_IN_NS;
		tmp1 = pps->CNTR_UTCHI;
		tmp2 = pps->CNTR_UTCLO;
	} while((tmp1 != tai_h) || (tmp2 != tai_l));

	tai = (uint64_t)(tai_h) << 32 | tai_l;

	tv->tv_sec = tai;
	tv->tv_usec = nsec / 1000;

	return tai;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
int get_kern_leaps(void)
{
	struct timex tx = {0};
	int *p;
	if (adjtimex(&tx) < 0) {
		fprintf(stderr, "%s: adjtimex(): %s\n", prgname,
			strerror(errno));
			return 0;
	}

	p = (int *)(&tx.stbcnt) + 1;
	return *p;
	//return t.tai;
}


131
int wrdate_get(volatile struct PPSG_WB *pps, int tohost)
132
{
133 134
	unsigned long taih, tail, nsec, tmp1, tmp2;
	uint64_t tai;
135
	time_t t;
136
	struct timeval hw,sw;
137
	struct tm tm;
138 139 140
	char utcs[64], tais[64];
	int tai_offset;

141
	tai_offset = get_kern_leaps();
142 143

	if (opt_not) {
144
		gettimeofday(&sw, NULL);
145
		taih = 0;
146 147
		tail = sw.tv_sec + tai_offset;
		nsec = sw.tv_usec * 1000;
148
	} else {
149
		taih = pps->CNTR_UTCHI;
150 151

		do {
152
			tail = pps->CNTR_UTCLO;
153 154 155
			nsec = pps->CNTR_NSEC * 16; /* we count a 16.5MHz */
			tmp1 = pps->CNTR_UTCHI;
			tmp2 = pps->CNTR_UTCLO;
156 157
		} while((tmp1 != taih) || (tmp2 != tail));
	}
158
	gettimeofday(&sw, NULL);
159 160 161 162 163

	tai = (uint64_t)(taih) << 32 | tail;

	/* Before printing (which takes time), set host time if so asked to */
	if (tohost) {
164 165 166
		hw.tv_sec = tai - tai_offset;
		hw.tv_usec = nsec / 1000;
		if (settimeofday(&hw, NULL))
167 168
			fprintf(stderr, "wr_date: settimeofday(): %s\n",
				strerror(errno));
169 170
	}

171
	t = tai; gmtime_r(&t, &tm);
172
	strftime(tais, sizeof(tais), "%Y-%m-%d %H:%M:%S", &tm);
173
	t -= tai_offset; gmtime_r(&t, &tm);
174
	strftime(utcs, sizeof(utcs), "%Y-%m-%d %H:%M:%S", &tm);
175
	printf("%lli.%09li TAI\n"
176 177
	       "%s.%09li TAI\n"
	       "%s.%09li UTC\n", tai, nsec, tais, nsec, utcs, nsec);
178 179 180 181 182 183 184
	if(opt_verbose)
	{
		gmtime_r(&(sw.tv_sec), &tm);
		strftime(utcs, sizeof(utcs), "%Y-%m-%d %H:%M:%S", &tm);
		printf("%s.%09li UTC (linux)\n", utcs, sw.tv_usec*1000);
	}

185 186 187
	return 0;
}

188

189 190 191
int __wrdate_diff(volatile struct PPSG_WB *pps,struct timeval *ht, struct timeval *wt) {
	struct timeval diff;
	int neg=0;
192

193
	neg=timeval_subtract(&diff, wt, ht);
194

195 196
	printf("%s%c%li.%06li\n",opt_verbose?("TAI(HW)-UTC(SW): "):(""),neg?'-':'+',labs(diff.tv_sec),labs(diff.tv_usec));
	if(opt_verbose)
197 198
	{

199
		wt->tv_sec-=get_kern_leaps(); //Convert HW clock from TAI to UTC
200

201 202
		neg=timeval_subtract(&diff, wt, ht);
		printf("UTC(HW)-UTC(SW): %c%li.%06li\n",neg?'-':'+',labs(diff.tv_sec),labs(diff.tv_usec));
203
	}
204
	return 0;
205 206 207

}

208
int wrdate_diff(volatile struct PPSG_WB *pps)
209
{
210
	struct timeval ht, wt;
211

212 213
	gettimeof_wr(&wt, pps);
	gettimeofday(&ht, NULL);
214

215
	return __wrdate_diff(pps,&ht, &wt);
216 217
}

218 219
#define ADJ_SEC_ITER 10

220
static int wait_wr_adjustment(volatile struct PPSG_WB *pps)
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
	int count=0;

	while ((pps->CR & PPSG_CR_CNT_ADJ)==0) {
		if ( (count++)>=ADJ_SEC_ITER ) {
			fprintf(stderr, "%s: warning: WR time adjustment not finished after %ds !!!\n",prgname,ADJ_SEC_ITER);
			return 0;
		}
		if (opt_verbose)
			printf("WR time adjustment: waiting.\n");
		sleep(1);
	}
	if (opt_verbose)
		printf("WR time adjustment: done.\n");
	return 1;
}

#define CLOCK_SOURCE_MODULE_NAME "wr_clocksource"
#define CLOCK_SOURCE_MODULE_ELF  "/wr/lib/modules/" CLOCK_SOURCE_MODULE_NAME ".ko"

int removeClockSourceModule(void) {
  int ret= delete_module(CLOCK_SOURCE_MODULE_NAME, 0);
  if ( ret<0 ) {
		fprintf(stderr, "%s: Warning: Cannot remove module "CLOCK_SOURCE_MODULE_NAME " : error=%s\n" ,
				prgname,
				strerror(errno));
		return 0;
  }
  if (opt_verbose) {
	  printf("Driver module "CLOCK_SOURCE_MODULE_NAME" removed.\n");
  }
  return 1;
}

int installClockSourceModule(void) {
    struct stat st;
    void *image=NULL;
    int fd;
    int ret=0;
    const char *params="";

    if ((fd = open(CLOCK_SOURCE_MODULE_ELF, O_RDONLY))<0 ) {
		fprintf(stderr, "%s: Warning: Cannot open file "CLOCK_SOURCE_MODULE_ELF " : error=%s\n" ,
				prgname,
				strerror(errno));
		goto out;;
    }
    if ( fstat(fd, &st)<0 ) {
		fprintf(stderr, "%s: Warning: Cannot stat file "CLOCK_SOURCE_MODULE_ELF " : error=%s\n" ,
				prgname,
				strerror(errno));
		goto out;
    }
    if ( (image = malloc(st.st_size)) ==NULL ) {
		fprintf(stderr, "%s: Warning: Cannot allocate memory : error=%s\n" ,
				prgname,
				strerror(errno));
		goto out;
    }
    if ( read(fd, image, st.st_size)< 0 ) {
		fprintf(stderr, "%s: Warning: Cannot read file "CLOCK_SOURCE_MODULE_ELF " : error=%s\n" ,
				prgname,
				strerror(errno));
		goto out;
    }
    if (init_module(image, st.st_size, params) != 0) {
		fprintf(stderr, "%s: Warning: Cannot init module "CLOCK_SOURCE_MODULE_NAME " : error=%s\n" ,
				prgname,
				strerror(errno));
		goto out;
    }
    ret=1;

294 295 296 297
    if (opt_verbose) {
  	  printf("Driver module "CLOCK_SOURCE_MODULE_NAME" installed.\n");
    }

298 299 300 301 302 303 304 305
    out:;
    if ( fd >=0 )
    	close(fd);
    if ( image!=NULL)
    	free(image);
    return ret;
}

306
/* This sets WR time from host time */
Jean-Claude BAU's avatar
Jean-Claude BAU committed
307
int __wrdate_internal_set(volatile struct PPSG_WB *pps, int adjSecOnly, int tai_offset, int deep)
308

309 310
{
	struct timeval tvh, tvr; /* host, rabbit */
311
	signed long long diff64;
312
	signed long diff;
313
	int modRemoved=0;
314

315 316
	if ( deep > 4 )
		return 0; /* Avoid stack overflow (recursive function) in case of error */
317

Jean-Claude BAU's avatar
Jean-Claude BAU committed
318
	if ( ! opt_not) {
319 320 321
		if ( deep==0) {
			modRemoved=removeClockSourceModule(); // The driver must be removed otherwise the time cannot be set properly
		}
322

323 324 325
		usleep(100);
		gettimeofday(&tvh, NULL);
		gettimeof_wr(&tvr, pps);
326

327 328 329 330 331 332 333 334 335 336 337
		/* diff is the expected step to be added, so host - WR */
		diff = tvh.tv_usec - tvr.tv_usec;
		diff64 = tvh.tv_sec + tai_offset - tvr.tv_sec;
		if (diff > 500 * 1000) {
			diff64++;
			diff -= 1000 * 1000;
		}
		if (diff < -500 * 1000) {
			diff64--;
			diff += 1000 * 1000;
		}
338

339 340 341 342 343 344 345 346 347 348
		/* Warn if more than 200ms away */
		if (diff > 200 * 1000 || diff < -200 * 1000)
			fprintf(stderr, "%s: Warning: fractional second differs by"
				"more than 0.2 (%li ms)\n", prgname, diff / 1000);

		if (opt_verbose && deep==0) {
			printf("Host time: %9li.%06li\n", (long)(tvh.tv_sec),
				   (long)(tvh.tv_usec));
			printf("WR   time: %9li.%06li\n", (long)(tvr.tv_sec),
				   (long)(tvr.tv_usec));
349
			__wrdate_diff(pps,&tvh,&tvr);
350
		}
351

352 353 354 355 356 357 358 359 360
		if (diff64) {
			if (opt_verbose)
				printf("adjusting by %lli seconds\n", diff64);
			/* We must write a signed "adjustment" value */
			pps->ADJ_UTCLO = diff64 & 0xffffffff;
			pps->ADJ_UTCHI = (diff64 >> 32) & 0xff;
			pps->ADJ_NSEC = 0;
			asm("" : : : "memory"); /* barrier... */
			pps->CR = pps->CR | PPSG_CR_CNT_ADJ;
361
			if ( wait_wr_adjustment(pps) && !adjSecOnly )
Jean-Claude BAU's avatar
Jean-Claude BAU committed
362
					__wrdate_internal_set(pps,0,tai_offset,deep+1); /* adjust the usecs */
363
		} else if ( !adjSecOnly ) {
364 365 366 367 368 369 370 371 372
			if (opt_verbose)
				printf("adjusting by %li usecs\n", diff);
			pps->ADJ_UTCLO = 0;
			pps->ADJ_UTCHI = 0;
			pps->ADJ_NSEC = (diff*1000)/16;
			asm("" : : : "memory"); /* barrier... */
			pps->CR = pps->CR | PPSG_CR_CNT_ADJ;
			wait_wr_adjustment(pps);
		}
373

374 375 376 377
		if ( deep==0 && modRemoved ) {
			installClockSourceModule();
		}
	}
378 379 380 381 382 383 384 385 386
	if (opt_verbose && deep==0) {
		usleep(100);
		gettimeofday(&tvh, NULL);
		gettimeof_wr(&tvr, pps);

		printf("Host time: %9li.%06li\n", (long)(tvh.tv_sec),
		       (long)(tvh.tv_usec));
		printf("WR   time: %9li.%06li\n", (long)(tvr.tv_sec),
		       (long)(tvr.tv_usec));
387
		__wrdate_diff(pps,&tvh,&tvr);
388 389 390 391
	}
	return 0;
}

392
/* This sets WR time from host time */
Jean-Claude BAU's avatar
Jean-Claude BAU committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
int wrdate_internal_set(volatile struct PPSG_WB *pps, int taiOnly, int adjSecOnly) {
	int tai_offset;

	if (opt_not) {
		// Do not change the TAI but display only information if verbose is enabled
		struct timex t;
		int taiOffset, hasExpired;

		if (!opt_verbose) return 0;
		/* first: get the current offset */
		memset(&t, 0, sizeof(t));
		if (adjtimex(&t) < 0) {
			fprintf(stderr, "%s: adjtimex(): %s\n", __func__,
				strerror(errno));
			return 0;
		}
		printf("Current TAI offset= %d\n",t.tai);
410
		if ( (taiOffset=getTaiOffsetFromLeapSecondsFile(NULL,time(NULL),NULL,&hasExpired))==-1) {
Jean-Claude BAU's avatar
Jean-Claude BAU committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
			fprintf(stderr, "%s: Cannot fix read TAI offset from leap seconds file\n" ,prgname);
			return 0;
		}
		printf("TAI offset form leap seconds file= %d\n",taiOffset);
		if ( hasExpired )
			printf("Leap seconds file has expired!\n");
	} else {
		if ( (tai_offset=fixHostTai(NULL,time(NULL),NULL,opt_verbose)) == -1 ) {
			fprintf(stderr, "%s: Cannot fix TAI offset\n" ,prgname);
			return 0;
		}
	}
	if ( taiOnly )
		return 0;
	return __wrdate_internal_set(pps,adjSecOnly,tai_offset,0);
426 427 428 429 430 431 432 433 434 435
}

/* This sets WR time from host time for grand master mode
 * For a GM we adjust only the seconds part of the time.
 */
#define FULL_SEC (1000*1000) /* 1000 ms */
#define HALF_SEC (FULL_SEC/2) /* 500 ms */
#define LOW_LIMIT_HALF_SEC (300*1000) /* 300 ms */
#define HIGH_LIMIT_HALF_SEC (700*1000) /* 300 ms */

Jean-Claude BAU's avatar
Jean-Claude BAU committed
436
int wrdate_internal_set_gm(volatile struct PPSG_WB *pps, int taiOnly) {
437 438 439 440 441 442 443 444 445 446

	if (opt_verbose ) {
		printf("Set WR time for grand master.\n");
	}
	/* We try to set the seconds between to PPS ticks */
	while (1==1) {
		struct timeval wt;

		gettimeof_wr(&wt, pps);
		if ( wt.tv_usec>LOW_LIMIT_HALF_SEC && wt.tv_usec<HIGH_LIMIT_HALF_SEC ) {
Jean-Claude BAU's avatar
Jean-Claude BAU committed
447
			wrdate_internal_set(pps,taiOnly,1);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			return 0;
		} else {
			useconds_t usec;

			usec= ( wt.tv_usec>HALF_SEC ) ?
					(FULL_SEC-wt.tv_usec)+HALF_SEC :
					HALF_SEC-wt.tv_usec;

			usleep(usec);
		}
	}
	return 0;
}

int getTimingMode(void) {
463 464
	static int connected=FALSE;
	struct rts_pll_state pstate;
465

466 467
	if ( !connected ) {
		if(	rts_connect(NULL) < 0)
468
			return -1;
469
		connected=TRUE;
470
	}
471 472 473
	if (rts_get_state(&pstate)<0 )
		return -1;
	return pstate.mode;
474 475
}

476
/* Frontend to the set mechanism: parse the argument */
477
int wrdate_set(volatile struct PPSG_WB *pps, int argc, char **argv)
478 479 480 481 482
{
	char *s;
	unsigned long t; /* WARNING: 64 bit */
	struct timeval tv;

Jean-Claude BAU's avatar
Jean-Claude BAU committed
483
	if (argc>=1 && !strcmp(argv[0], "host")) {
484
		int tm=getTimingMode();
Jean-Claude BAU's avatar
Jean-Claude BAU committed
485 486 487 488
		int taiOnly;

		taiOnly=argc>=2 && !strcmp(argv[1], "tai");

489
		switch (tm) {
490
		case SPLL_MODE_GRAND_MASTER:
Jean-Claude BAU's avatar
Jean-Claude BAU committed
491
			return wrdate_internal_set_gm(pps,taiOnly);
492 493
		case SPLL_MODE_FREE_RUNNING_MASTER :
		case SPLL_MODE_DISABLED :
Jean-Claude BAU's avatar
Jean-Claude BAU committed
494
			return wrdate_internal_set(pps,taiOnly,0);
495 496
			break;
		case SPLL_MODE_SLAVE :
Jean-Claude BAU's avatar
Jean-Claude BAU committed
497
			fprintf(stderr, "Slave timing mode: WR time and TAI offset cannot be set!!!\n");
498 499
			return -1;
		default:
Jean-Claude BAU's avatar
Jean-Claude BAU committed
500
			fprintf(stderr, "Cannot read Soft PLL timing mode. WR time and TAI offset cannot be set (ret=%d)\n",tm);
501 502 503
			return -1;
		}
	}
504

Jean-Claude BAU's avatar
Jean-Claude BAU committed
505 506 507 508 509 510 511 512 513 514 515
	if ( argc>=1 ) {
		s = strdup(argv[0]);
		if (sscanf(argv[0], "%li%s", &t, s) == 1) {
			tv.tv_sec = t;
			tv.tv_usec = 0;
			if (settimeofday(&tv, NULL) < 0) {
				fprintf(stderr, "%s: settimeofday(%s): %s\n",
					prgname, argv[0], strerror(errno));
				exit(1);
			}
			return wrdate_internal_set(pps,1,0);
516 517
		}

Jean-Claude BAU's avatar
Jean-Claude BAU committed
518 519 520 521 522
		/* FIXME: other time formats */
		printf(" FIXME\n");
		return 0;
	}
	fprintf(stderr, "Missing parameter!! \n\n");
523 524 525
	return 0;
}

526 527 528
/* Print statistics between TAI and UTC dates */
#define STAT_SAMPLE_COUNT 20

529
int wrdate_stat(volatile struct PPSG_WB *pps)
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
{
	int udiff_ref=0,udiff_last;

	printf("Diff_TAI_UTC[sec] Diff_with_last[usec] Diff_with_ref[usec]\n");
	while ( 1 ) {
		int64_t udiff_arr[STAT_SAMPLE_COUNT]; // Diff in useconds
		struct timeval tv_tai,tv_host;
		int i;
		int64_t udiff_sum=0, udiff;

		for ( i=0; i<STAT_SAMPLE_COUNT; i++ ) {
			int64_t *udiff_tmp=&udiff_arr[i];

			// Get time
			usleep(100); // Increase stability of measures : less preempted during time measures
			gettimeof_wr(&tv_tai,pps);
			gettimeofday(&tv_host, NULL);

			// Calculate difference
			*udiff_tmp=(tv_host.tv_sec-tv_tai.tv_sec)*1000000;
			if ( tv_host.tv_usec > tv_tai.tv_usec ) {
				*udiff_tmp+=tv_host.tv_usec-tv_tai.tv_usec;
			} else {
				*udiff_tmp-=tv_tai.tv_usec-tv_host.tv_usec;
			}
			udiff_sum+=*udiff_tmp;
		}
		udiff=udiff_sum/STAT_SAMPLE_COUNT;
		if ( udiff_ref==0) {
			udiff_ref=udiff_last=udiff;
		}

		printf("%03d.%06d %6li %6li\n",
			   (int)(udiff/1000000),
			   abs(udiff%1000000),
			   (long) (udiff_last-udiff),
			   (long) (udiff_ref-udiff)
			   );
		udiff_last=udiff;

		sleep(1);
	}

	return 0;
}


577 578
int main(int argc, char **argv)
{
579
	int c, tohost = 0;
580
	char *cmd;
581
	volatile struct PPSG_WB *pps;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

	prgname = argv[0];

	while ( (c = getopt(argc, argv, "fc:vn")) != -1) {
		switch(c) {
		case 'f':
			opt_force = 1;
			break;
		case 'c':
			opt_cfgfile = optarg;
			break;
		case 'v':
			opt_verbose = 1;
			break;
		case 'n':
			opt_not = 1;
Jean-Claude BAU's avatar
Jean-Claude BAU committed
598
			printf("Dry run mode: No action will be performed\n");
599 600
			break;
		default:
Jean-Claude BAU's avatar
Jean-Claude BAU committed
601
			help();
602 603 604
		}
	}
	if (optind > argc - 1)
Jean-Claude BAU's avatar
Jean-Claude BAU committed
605
		help();
606 607 608

	cmd = argv[optind++];

609 610 611 612
	wrdate_check_host();
	pps = create_map(FPGA_BASE_PPSG, sizeof(*pps));
	if (!pps) {
		fprintf(stderr, "%s: mmap: %s\n", prgname, strerror(errno));
613 614 615 616 617 618
		exit(1);
	}

	wrdate_cfgfile(opt_cfgfile);

	if (!strcmp(cmd, "get")) {
619 620 621 622
		/* parse the optional "tohost" argument */
		if (optind == argc - 1 && !strcmp(argv[optind], "tohost"))
			tohost = 1;
		else if (optind < argc)
Jean-Claude BAU's avatar
Jean-Claude BAU committed
623
			help();
624
		return wrdate_get(pps, tohost);
625 626
	}

627 628 629 630
	if (!strcmp(cmd, "diff")) {
		return wrdate_diff(pps);
	}

631 632 633 634 635
	if (!strcmp(cmd, "stat")) {
		/* parse the optional "tohost" argument */
		return wrdate_stat(pps);
	}

636 637
	/* only other command is "set", with one on more arguments */
	if (strcmp(cmd, "set") || optind > argc - 1)
Jean-Claude BAU's avatar
Jean-Claude BAU committed
638
		help();
639

640

641
	return wrdate_set(pps, argc-optind,&argv[optind]);
642
}