18c2ecf20Sopenharmony_ci/* Set tai offset
28c2ecf20Sopenharmony_ci *              by: John Stultz <john.stultz@linaro.org>
38c2ecf20Sopenharmony_ci *              (C) Copyright Linaro 2013
48c2ecf20Sopenharmony_ci *              Licensed under the GPLv2
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *   This program is free software: you can redistribute it and/or modify
78c2ecf20Sopenharmony_ci *   it under the terms of the GNU General Public License as published by
88c2ecf20Sopenharmony_ci *   the Free Software Foundation, either version 2 of the License, or
98c2ecf20Sopenharmony_ci *   (at your option) any later version.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
128c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148c2ecf20Sopenharmony_ci *   GNU General Public License for more details.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <stdio.h>
198c2ecf20Sopenharmony_ci#include <stdlib.h>
208c2ecf20Sopenharmony_ci#include <time.h>
218c2ecf20Sopenharmony_ci#include <sys/time.h>
228c2ecf20Sopenharmony_ci#include <sys/timex.h>
238c2ecf20Sopenharmony_ci#include <string.h>
248c2ecf20Sopenharmony_ci#include <signal.h>
258c2ecf20Sopenharmony_ci#include <unistd.h>
268c2ecf20Sopenharmony_ci#include "../kselftest.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciint set_tai(int offset)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct timex tx;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	memset(&tx, 0, sizeof(tx));
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	tx.modes = ADJ_TAI;
358c2ecf20Sopenharmony_ci	tx.constant = offset;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return adjtimex(&tx);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciint get_tai(void)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct timex tx;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	memset(&tx, 0, sizeof(tx));
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	adjtimex(&tx);
478c2ecf20Sopenharmony_ci	return tx.tai;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint main(int argc, char **argv)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	int i, ret;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ret = get_tai();
558c2ecf20Sopenharmony_ci	printf("tai offset started at %i\n", ret);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	printf("Checking tai offsets can be properly set: ");
588c2ecf20Sopenharmony_ci	fflush(stdout);
598c2ecf20Sopenharmony_ci	for (i = 1; i <= 60; i++) {
608c2ecf20Sopenharmony_ci		ret = set_tai(i);
618c2ecf20Sopenharmony_ci		ret = get_tai();
628c2ecf20Sopenharmony_ci		if (ret != i) {
638c2ecf20Sopenharmony_ci			printf("[FAILED] expected: %i got %i\n", i, ret);
648c2ecf20Sopenharmony_ci			return ksft_exit_fail();
658c2ecf20Sopenharmony_ci		}
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci	printf("[OK]\n");
688c2ecf20Sopenharmony_ci	return ksft_exit_pass();
698c2ecf20Sopenharmony_ci}
70