Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 1 | /* |
| 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 Litvin | 0c072ee | 2020-10-09 10:46:28 -0400 | [diff] [blame] | 22 | |
| 23 | #pragma once |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 24 | |
Kevin Schoedel | 5d905ea | 2022-01-21 15:03:15 -0500 | [diff] [blame] | 25 | #include <lib/support/CodeUtils.h> |
Sagar Dhawan | 7b20ccb | 2020-06-23 10:11:14 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
Andrei Litvin | bdde572 | 2020-06-23 14:36:16 -0400 | [diff] [blame] | 27 | #include <system/SystemClock.h> |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 28 | |
| 29 | namespace chip { |
| 30 | namespace Time { |
| 31 | |
| 32 | enum 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 | */ |
| 42 | template <Source kSource> |
| 43 | class TimeSource |
| 44 | { |
| 45 | public: |
| 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 Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 57 | System::Clock::Timestamp GetMonotonicTimestamp(); |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * A system time source, based on the system platform layer. |
| 62 | */ |
| 63 | template <> |
| 64 | class TimeSource<Source::kSystem> |
| 65 | { |
| 66 | public: |
Kevin Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 67 | System::Clock::Timestamp GetMonotonicTimestamp() { return System::SystemClock().GetMonotonicTimestamp(); } |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * A test time source. Allows setting the current time. |
| 72 | */ |
| 73 | template <> |
| 74 | class TimeSource<Source::kTest> |
| 75 | { |
| 76 | public: |
Kevin Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 77 | System::Clock::Timestamp GetMonotonicTimestamp() { return mCurrentTime; } |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 78 | |
Kevin Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 79 | void SetMonotonicTimestamp(System::Clock::Timestamp value) |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 80 | { |
Kevin Schoedel | 5d905ea | 2022-01-21 15:03:15 -0500 | [diff] [blame] | 81 | VerifyOrDie(value >= mCurrentTime); |
Kevin Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 82 | mCurrentTime = value; |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
Kevin Schoedel | 618763d | 2021-11-05 10:32:15 -0400 | [diff] [blame] | 86 | System::Clock::Timestamp mCurrentTime = System::Clock::kZero; |
Andrei Litvin | 391f2ef | 2020-06-17 11:55:34 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace Time |
| 90 | } // namespace chip |