blob: 308e82122fc5b8b58dd5b65dd574ddcdbbc1d048 [file] [log] [blame]
Pau Espin Pedrol87fade82018-02-26 19:42:22 +01001/*
2 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * Authors: Holger Hans Peter Freyther <zecke@selfish.org>
7 * Pablo Neira Ayuso <pablo@gnumonks.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
29
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/timer.h>
32#include <osmocom/core/select.h>
33#include <osmocom/core/linuxlist.h>
34#include <osmocom/core/timer_compat.h>
35
36int main(int argc, char *argv[])
37{
38
39 struct timespec ts1 = { 123, 456 }, ts2 = {1, 200};
40 struct timespec read1, read2, res;
41 struct timespec *mono;
42
43 osmo_clock_gettime(CLOCK_BOOTTIME, &read1);
44 usleep(500);
45 osmo_clock_gettime(CLOCK_BOOTTIME, &read2);
46 if (!timespeccmp(&read2, &read1, >))
47 return EXIT_FAILURE;
48 printf("Non implemented clocks work fine\n");
49
50 osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
51 usleep(500);
52 osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
53 if (!timespeccmp(&read2, &read1, >))
54 return EXIT_FAILURE;
55 printf("Monotonic clock is working fine by default\n");
56
57 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
58 printf("Monotonic clock override enabled\n");
59
60 mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
61 if (timespecisset(mono))
62 return EXIT_FAILURE;
63 printf("Monotonic override is cleared by default\n");
64
65 memcpy(mono, &ts1, sizeof(struct timespec));
66 osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
67 if (!timespeccmp(&ts1, &read1, ==))
68 return EXIT_FAILURE;
69 printf("Monotonic clock can be overriden\n");
70
71 osmo_clock_override_add(CLOCK_MONOTONIC, ts2.tv_sec, ts2.tv_nsec);
72 osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
73 timespecadd(&ts2, &ts1, &res);
74 if (!timespeccmp(&res, &read1, ==))
75 return EXIT_FAILURE;
76 printf("osmo_clock_override_add works fine.\n");
77
78 osmo_clock_override_enable(CLOCK_MONOTONIC, false);
79 printf("Monotonic clock override disabled\n");
80
81 osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
82 usleep(500);
83 osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
84 if (!timespeccmp(&read2, &read1, >))
85 return EXIT_FAILURE;
86 printf("Monotonic clock is working fine after enable+disable.\n");
87
88 return 0;
89}