summaryrefslogtreecommitdiffstats
path: root/tests/timer/clk_override_test.c
blob: 308e82122fc5b8b58dd5b65dd574ddcdbbc1d048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * Authors: Holger Hans Peter Freyther <zecke@selfish.org>
 *	    Pablo Neira Ayuso <pablo@gnumonks.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <osmocom/core/talloc.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/timer_compat.h>

int main(int argc, char *argv[])
{

	struct timespec ts1 = { 123, 456 }, ts2 = {1, 200};
	struct timespec read1, read2, res;
	struct timespec *mono;

	osmo_clock_gettime(CLOCK_BOOTTIME, &read1);
	usleep(500);
	osmo_clock_gettime(CLOCK_BOOTTIME, &read2);
	if (!timespeccmp(&read2, &read1, >))
		return EXIT_FAILURE;
	printf("Non implemented clocks work fine\n");

	osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
	usleep(500);
	osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
	if (!timespeccmp(&read2, &read1, >))
		return EXIT_FAILURE;
	printf("Monotonic clock is working fine by default\n");

	osmo_clock_override_enable(CLOCK_MONOTONIC, true);
	printf("Monotonic clock override enabled\n");

	mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
	if (timespecisset(mono))
		return EXIT_FAILURE;
	printf("Monotonic override is cleared by default\n");

	memcpy(mono, &ts1, sizeof(struct timespec));
	osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
	if (!timespeccmp(&ts1, &read1, ==))
		return EXIT_FAILURE;
	printf("Monotonic clock can be overriden\n");

	osmo_clock_override_add(CLOCK_MONOTONIC, ts2.tv_sec, ts2.tv_nsec);
	osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
	timespecadd(&ts2, &ts1, &res);
	if (!timespeccmp(&res, &read1, ==))
		return EXIT_FAILURE;
	printf("osmo_clock_override_add works fine.\n");

	osmo_clock_override_enable(CLOCK_MONOTONIC, false);
	printf("Monotonic clock override disabled\n");

	osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
	usleep(500);
	osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
	if (!timespeccmp(&read2, &read1, >))
		return EXIT_FAILURE;
	printf("Monotonic clock is working fine after enable+disable.\n");

	return 0;
}