Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 1 | /* |
| 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 | * |
Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <osmocom/core/talloc.h> |
| 27 | #include <osmocom/core/timer.h> |
| 28 | #include <osmocom/core/select.h> |
| 29 | #include <osmocom/core/linuxlist.h> |
| 30 | #include <osmocom/core/timer_compat.h> |
| 31 | |
| 32 | int main(int argc, char *argv[]) |
| 33 | { |
| 34 | |
| 35 | struct timespec ts1 = { 123, 456 }, ts2 = {1, 200}; |
| 36 | struct timespec read1, read2, res; |
| 37 | struct timespec *mono; |
| 38 | |
| 39 | osmo_clock_gettime(CLOCK_BOOTTIME, &read1); |
| 40 | usleep(500); |
| 41 | osmo_clock_gettime(CLOCK_BOOTTIME, &read2); |
| 42 | if (!timespeccmp(&read2, &read1, >)) |
| 43 | return EXIT_FAILURE; |
| 44 | printf("Non implemented clocks work fine\n"); |
| 45 | |
| 46 | osmo_clock_gettime(CLOCK_MONOTONIC, &read1); |
| 47 | usleep(500); |
| 48 | osmo_clock_gettime(CLOCK_MONOTONIC, &read2); |
| 49 | if (!timespeccmp(&read2, &read1, >)) |
| 50 | return EXIT_FAILURE; |
| 51 | printf("Monotonic clock is working fine by default\n"); |
| 52 | |
| 53 | osmo_clock_override_enable(CLOCK_MONOTONIC, true); |
| 54 | printf("Monotonic clock override enabled\n"); |
| 55 | |
| 56 | mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC); |
| 57 | if (timespecisset(mono)) |
| 58 | return EXIT_FAILURE; |
| 59 | printf("Monotonic override is cleared by default\n"); |
| 60 | |
| 61 | memcpy(mono, &ts1, sizeof(struct timespec)); |
| 62 | osmo_clock_gettime(CLOCK_MONOTONIC, &read1); |
| 63 | if (!timespeccmp(&ts1, &read1, ==)) |
| 64 | return EXIT_FAILURE; |
| 65 | printf("Monotonic clock can be overriden\n"); |
| 66 | |
| 67 | osmo_clock_override_add(CLOCK_MONOTONIC, ts2.tv_sec, ts2.tv_nsec); |
| 68 | osmo_clock_gettime(CLOCK_MONOTONIC, &read1); |
| 69 | timespecadd(&ts2, &ts1, &res); |
| 70 | if (!timespeccmp(&res, &read1, ==)) |
| 71 | return EXIT_FAILURE; |
| 72 | printf("osmo_clock_override_add works fine.\n"); |
| 73 | |
| 74 | osmo_clock_override_enable(CLOCK_MONOTONIC, false); |
| 75 | printf("Monotonic clock override disabled\n"); |
| 76 | |
| 77 | osmo_clock_gettime(CLOCK_MONOTONIC, &read1); |
| 78 | usleep(500); |
| 79 | osmo_clock_gettime(CLOCK_MONOTONIC, &read2); |
| 80 | if (!timespeccmp(&read2, &read1, >)) |
| 81 | return EXIT_FAILURE; |
| 82 | printf("Monotonic clock is working fine after enable+disable.\n"); |
| 83 | |
| 84 | return 0; |
| 85 | } |