blob: 64fa436337694fd735763aa1ac73cf28af38c886 [file] [log] [blame]
Andrei Litvin391f2ef2020-06-17 11:55:34 -04001/*
2 *
3 * Copyright (c) 2020 Project CHIP Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @brief defines a generic time source interface that uses a real clock
20 * at runtime but can be substituted by a test one for unit tests.
21 */
Andrei Litvin0c072ee2020-10-09 10:46:28 -040022
23#pragma once
Andrei Litvin391f2ef2020-06-17 11:55:34 -040024
Kevin Schoedel5d905ea2022-01-21 15:03:15 -050025#include <lib/support/CodeUtils.h>
Sagar Dhawan7b20ccb2020-06-23 10:11:14 -070026#include <stdlib.h>
Andrei Litvinbdde5722020-06-23 14:36:16 -040027#include <system/SystemClock.h>
Andrei Litvin391f2ef2020-06-17 11:55:34 -040028
29namespace chip {
30namespace Time {
31
32enum class Source
33{
34 kSystem, // System time source
35 kTest, // Test time source
36};
37
38/**
39 * Defines a generic time source within a system. System time and test times
40 * are available.
41 */
42template <Source kSource>
43class TimeSource
44{
45public:
46 /**
47 * Returns a monotonically increasing time in milliseconds since an arbitrary, platform-defined
48 * epoch.
49 *
50 * Maintains requirements for the System::Platform::Layer clock implementation:
51 *
52 * - Return a value that is ever-increasing (i.e. never * wraps) between reboots of the system.
53 * - The underlying time source is required to tick continuously during any system sleep modes
54 * such that the values do not entail a restart upon wake.
55 * - This function is expected to be thread-safe on any platform that employs threading.
56 */
Kevin Schoedel618763d2021-11-05 10:32:15 -040057 System::Clock::Timestamp GetMonotonicTimestamp();
Andrei Litvin391f2ef2020-06-17 11:55:34 -040058};
59
60/**
61 * A system time source, based on the system platform layer.
62 */
63template <>
64class TimeSource<Source::kSystem>
65{
66public:
Kevin Schoedel618763d2021-11-05 10:32:15 -040067 System::Clock::Timestamp GetMonotonicTimestamp() { return System::SystemClock().GetMonotonicTimestamp(); }
Andrei Litvin391f2ef2020-06-17 11:55:34 -040068};
69
70/**
71 * A test time source. Allows setting the current time.
72 */
73template <>
74class TimeSource<Source::kTest>
75{
76public:
Kevin Schoedel618763d2021-11-05 10:32:15 -040077 System::Clock::Timestamp GetMonotonicTimestamp() { return mCurrentTime; }
Andrei Litvin391f2ef2020-06-17 11:55:34 -040078
Kevin Schoedel618763d2021-11-05 10:32:15 -040079 void SetMonotonicTimestamp(System::Clock::Timestamp value)
Andrei Litvin391f2ef2020-06-17 11:55:34 -040080 {
Kevin Schoedel5d905ea2022-01-21 15:03:15 -050081 VerifyOrDie(value >= mCurrentTime);
Kevin Schoedel618763d2021-11-05 10:32:15 -040082 mCurrentTime = value;
Andrei Litvin391f2ef2020-06-17 11:55:34 -040083 }
84
85private:
Kevin Schoedel618763d2021-11-05 10:32:15 -040086 System::Clock::Timestamp mCurrentTime = System::Clock::kZero;
Andrei Litvin391f2ef2020-06-17 11:55:34 -040087};
88
89} // namespace Time
90} // namespace chip